PHP的职责链设计模式
PHP实现职责链设计模式
参考文章地址:深入聊聊设计模式利器之“职责链模式”(附go实现流程)
实现原理看参考文章就好了 原文是用 go 语言去实现,这里写一个 php 版本的实现方式,框架用的 hyperf。
文件结构:
IndexController 为调用端,UserInfoEntity 用户实体用于存用户信息,flow 里面的为各种处理流程
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 方法,结果如下:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanfbjgk
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24