PHP如何实现职责链设计模式

2023-02-08 22:45:45 8
魁首哥

PHP如何实现职责链设计模式

这篇文章主要介绍“PHP如何实现职责链设计模式”,在日常操作中,相信很多人在PHP如何实现职责链设计模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PHP如何实现职责链设计模式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

PHP实现职责链设计模式

文件结构:

IndexController 为调用端,UserInfoEntity 用户实体用于存用户信息,flow 里面的为各种处理流程

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);    }}

写一个单元测试跑一下 indexController 的 index 方法,结果如下:

到此,关于“PHP如何实现职责链设计模式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注恰卡编程网网站,小编会继续努力为大家带来更多实用的文章!

分享
海报
8
上一篇:Go语言如何判断指定字符是否存在 下一篇:node消息队列怎么使用

忘记密码?

图形验证码