Cet article vous apporte des connaissances pertinentes sur les modèles de conception PHP. Il présente principalement comment PHP implémente le modèle de conception de chaîne de responsabilité. J'espère qu'il sera utile aux amis dans le besoin.
Adresse de l'article de référence : Parlons en profondeur de l'outil de design pattern « Chain of Responsibility Pattern » (avec processus d'implémentation go)
Il suffit de lire l'article de référence pour le principe d'implémentation. Le texte original est Utiliser le langage Go pour l'implémenter. Voici une version PHP de l'implémentation. Le framework utilise hyperf.
Structure du fichier :
IndexController est l'extrémité appelante, l'entité utilisateur UserInfoEntity est utilisée pour stocker les informations utilisateur et le flux contient divers processus de traitement
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); }
AbstractHand ler
<?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); } }
Réception
<?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); } }
Écrivez un test unitaire et exécutez la méthode d'indexation d'indexController Les résultats sont les suivants :
Étude recommandée : " Vidéo PHP Tutoriel" 》
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!