温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP如何实现职责链设计模式

发布时间:2023-02-06 10:58:00 来源:亿速云 阅读:123 作者:iii 栏目:编程语言

这篇文章主要介绍“PHP如何实现职责链设计模式”,在日常操作中,相信很多人在PHP如何实现职责链设计模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PHP如何实现职责链设计模式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

PHP实现职责链设计模式

文件结构:

IndexController 为调用端,UserInfoEntity 用户实体用于存用户信息,flow 里面的为各种处理流程

PHP如何实现职责链设计模式

IndexController

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;
use App\Service\Entity\UserInfoEntity;
use App\Service\Flow\Cashier;
use App\Service\Flow\Clinic;
use App\Service\Flow\Pharmacy;
use App\Service\Flow\Reception;
use App\Service\StartHandler;
class IndexController extends AbstractController
{
    public function index()
    {
        $startHandler = new StartHandler();
        $userInfo = (new UserInfoEntity())->setName('zhangsan');
        $startHandler->setNextHandler(new Reception())
            ->setNextHandler(new Clinic())
            ->setNextHandler(new Cashier())
            ->setNextHandler(new Pharmacy());
        $startHandler->execute($userInfo);
    }
}

UserInfoEntity

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Entity;
class UserInfoEntity
{
    private string $name;
    private bool $registrationDone = false;
    private bool $doctorCheckUpDone = false;
    private bool $medicineDone = false;
    private bool $paymentDone = false;
    public function getName(): string
    {
        return $this->name;
    }
    public function setName(string $name): UserInfoEntity
    {
        $this->name = $name;
        return $this;
    }
    public function isRegistrationDone(): bool
    {
        return $this->registrationDone;
    }
    public function setRegistrationDone(bool $registrationDone): UserInfoEntity
    {
        $this->registrationDone = $registrationDone;
        return $this;
    }
    public function isDoctorCheckUpDone(): bool
    {
        return $this->doctorCheckUpDone;
    }
    public function setDoctorCheckUpDone(bool $doctorCheckUpDone): UserInfoEntity
    {
        $this->doctorCheckUpDone = $doctorCheckUpDone;
        return $this;
    }
    public function isMedicineDone(): bool
    {
        return $this->medicineDone;
    }
    public function setMedicineDone(bool $medicineDone): UserInfoEntity
    {
        $this->medicineDone = $medicineDone;
        return $this;
    }
    public function isPaymentDone(): bool
    {
        return $this->paymentDone;
    }
    public function setPaymentDone(bool $paymentDone): UserInfoEntity
    {
        $this->paymentDone = $paymentDone;
        return $this;
    }
}

HandlerInterface

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;
use App\Service\Entity\UserInfoEntity;
interface HandlerInterface
{
    public function setNextHandler(HandlerInterface $handler): HandlerInterface;
    public function execute(UserInfoEntity $info);
    public function do(UserInfoEntity $info);
}

AbstractHandler

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;
use App\Service\Entity\UserInfoEntity;
class AbstractHandler implements HandlerInterface
{
    private HandlerInterface $nextHandler;
    public function setNextHandler(HandlerInterface $handler): HandlerInterface
    {
        $this->nextHandler = $handler;
        return $this->nextHandler;
    }
    public function execute(UserInfoEntity $info)
    {
        if (! empty($this->nextHandler)) {
            try {
                $this->nextHandler->do($info);
            } catch (\Exception $e) {
                return;
            }
            return $this->nextHandler->execute($info);
        }
    }
    public function do(UserInfoEntity $info)
    {
        // TODO: Implement do() method.
    }
}

StartHandler

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;
class StartHandler extends AbstractHandler
{
}

Cashier

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Cashier extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo '收费' . PHP_EOL;
        $info->setPaymentDone(true);
    }
}

Clinic

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Clinic extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo '诊室' . PHP_EOL;
        $info->setDoctorCheckUpDone(true);
    }
}

Pharmacy

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Pharmacy extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo '药房' . PHP_EOL;
        $info->setMedicineDone(true);
    }
}

Reception

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Flow;
// 挂号
use App\Service\AbstractHandler;
use App\Service\Entity\UserInfoEntity;
class Reception extends AbstractHandler
{
    public function do(UserInfoEntity $info)
    {
        echo '挂号' . PHP_EOL;
        $info->setRegistrationDone(true);
    }
}

写一个单元测试跑一下 indexController 的 index 方法,结果如下:

PHP如何实现职责链设计模式

到此,关于“PHP如何实现职责链设计模式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI