您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/2/28 7:29:02发布20次查看
本篇文章给大家带来了关于php设计模式的相关知识,其中主要介绍了php是怎么实现职责链设计模式的,下面一起来看一下,希望对需要的朋友有所帮助。
php实现职责链设计模式参考文章地址:深入聊聊设计模式利器之“职责链模式”(附go实现流程)
实现原理看参考文章就好了 原文是用 go 语言去实现,这里写一个 php 版本的实现方式,框架用的 hyperf。
文件结构:
indexcontroller 为调用端,userinfoentity 用户实体用于存用户信息,flow 里面的为各种处理流程
indexcontroller
<?phpdeclare(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
<?phpdeclare(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
<?phpdeclare(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
<?phpdeclare(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
<?phpdeclare(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
<?phpdeclare(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
<?phpdeclare(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
<?phpdeclare(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
<?phpdeclare(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实现职责链设计模式(附代码示例)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product