Home>Article>Backend Development> One article explains in detail how to implement chain of responsibility design pattern in PHP (with code examples)

One article explains in detail how to implement chain of responsibility design pattern in PHP (with code examples)

藏色散人
藏色散人 forward
2023-01-17 11:49:41 1871browse

This article brings you relevant knowledge about PHP design patterns. It mainly introduces how PHP implements the chain of responsibility design pattern. Let's take a look at it together. I hope it will be helpful to friends in need.

PHP implements the responsibility chain design pattern

Reference article address:Let’s talk in depth about the "responsibility chain pattern" of the design pattern tool (with go implementation Process)

Just read the reference article for the implementation principle. The original text is implemented in Go language. Here is a PHP version of the implementation, and the framework uses hyperf.

File structure:

IndexController is the calling end, UserInfoEntity user entity is used to store user information, and flow contains various processing processes

One article explains in detail how to implement chain of responsibility design pattern in PHP (with code examples)

IndexController

setName('zhangsan'); $startHandler->setNextHandler(new Reception()) ->setNextHandler(new Clinic()) ->setNextHandler(new Cashier()) ->setNextHandler(new Pharmacy()); $startHandler->execute($userInfo); } }

UserInfoEntity

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


      

AbstractHandler

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


      

Cashier

setPaymentDone(true); } }

Clinic

setDoctorCheckUpDone(true); } }

Pharmacy

setMedicineDone(true); } }

Reception

setRegistrationDone(true); } }

Write a unit test and run the index method of indexController. The results are as follows:

One article explains in detail how to implement chain of responsibility design pattern in PHP (with code examples)

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of One article explains in detail how to implement chain of responsibility design pattern in PHP (with code examples). For more information, please follow other related articles on the PHP Chinese website!

php
Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete