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

藏色散人
Release: 2023-04-11 10:04:02
forward
1774 people have browsed it

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);
    }
}
Copy after login

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;
    }
}
Copy after login

HandlerInterface

Copy after login

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.
    }
}
Copy after login

StartHandler

Copy after login

Cashier

setPaymentDone(true);
    }
}
Copy after login

Clinic

setDoctorCheckUpDone(true);
    }
}
Copy after login

Pharmacy

setMedicineDone(true);
    }
}
Copy after login

Reception

setRegistrationDone(true);
    }
}
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!