這篇文章主要介紹“PHP如何實現(xiàn)職責(zé)鏈設(shè)計模式”,在日常操作中,相信很多人在PHP如何實現(xiàn)職責(zé)鏈設(shè)計模式問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PHP如何實現(xiàn)職責(zé)鏈設(shè)計模式”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司專注于獨山子網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供獨山子營銷型網(wǎng)站建設(shè),獨山子網(wǎng)站制作、獨山子網(wǎng)頁設(shè)計、獨山子網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造獨山子網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供獨山子網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
文件結(jié)構(gòu):
IndexController 為調(diào)用端,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 方法,結(jié)果如下:
到此,關(guān)于“PHP如何實現(xiàn)職責(zé)鏈設(shè)計模式”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
分享文章:PHP如何實現(xiàn)職責(zé)鏈設(shè)計模式
地址分享:http://bm7419.com/article48/gocpep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航、微信小程序、云服務(wù)器、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)