2023-01-18

一文详解PHP实现职责链设计模式(附代码示例)

本篇文章给大家带来了关于PHP设计模式的相关知识,其中主要介绍了PHP是怎么实现职责链设计模式的,下面一起来看一下,希望对需要的朋友有所帮助。

PHP实现职责链设计模式

参考文章地址:深入聊聊设计模式利器之“职责链模式”(附go实现流程)

实现原理看参考文章就好了 原文是用 go 语言去实现,这里写一个 php 版本的实现方式,框架用的 hyperf。

文件结构:

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

8fbf1f3ee06110c577dd31976a58e62.jpg

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 方法,结果如下:

cec1aaca54e57f053b82c91f29a7a9a.jpg

推荐学习:《PHP视频教程

以上就是一文详解PHP实现职责链设计模式(附代码示例)的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:learnku,如有侵犯,请联系admin@php.cn删除

  • 相关标签:php 职责链
  • https://www.php.cn/php-weizijiaocheng-500116.html

    发表回复

    Your email address will not be published. Required fields are marked *