Rumah >pembangunan bahagian belakang >tutorial php >Satu artikel menerangkan secara terperinci cara melaksanakan corak reka bentuk rantaian tanggungjawab dalam PHP (dengan contoh kod)
Artikel ini membawakan anda pengetahuan yang berkaitan tentang corak reka bentuk PHP terutamanya cara PHP melaksanakan rantaian corak reka bentuk tanggungjawab. Saya harap ia dapat membantu rakan yang memerlukan.
Alamat artikel rujukan: Mari kita bincang secara mendalam tentang "corak rantai tanggungjawab", a alat corak reka bentuk (dengan Proses pelaksanaan go)
Cuma baca artikel rujukan untuk prinsip pelaksanaan Teks asal dilaksanakan dalam bahasa Go. Berikut ialah versi PHP pelaksanaan. Rangka kerja menggunakan hyperf.
Struktur fail:
IndexController ialah penghujung panggilan, entiti pengguna UserInfoEntity digunakan untuk menyimpan maklumat pengguna dan aliran mengandungi pelbagai proses pemprosesan
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. } }
<?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 { }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\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); } }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\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); } }Juruwang
<?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); } }Klinik
<?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); } }FarmasiSambutan
Tulis ujian unit dan jalankan indexController kaedah indeks, hasilnya adalah seperti berikut:
Atas ialah kandungan terperinci Satu artikel menerangkan secara terperinci cara melaksanakan corak reka bentuk rantaian tanggungjawab dalam PHP (dengan contoh kod). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!