常用设计模式之责任链模式及其PHP实现
责任链模式是一种行为型模式,它包含了一些命令对象和一系列的处理对象。每一个处理对象决定它能处理哪些命令对象,它也知道如何将它不能处理的命令对象传递给该链中的下一个处理对象。该模式还描述了往该处理链的末尾添加新的处理对象的方法。
主要角色抽象责任(Responsibility)角色:定义所有责任支持的公共方法。具体责任(Concrete Responsibility)角色:以抽象责任接口实现的具体责任责任链(Chain of responsibility)角色:设定责任的调用规则类图
<?phpabstract class Responsibility { // 抽象责任角色 protected $next; // 下一个责任角色 public function setNext(Responsibility $l) {$this->next = $l;return $this; } abstract public function operate(); // 操作方法} class ResponsibilityA extends Responsibility { public function __construct() {} public function operate(){if (false == is_null($this->next)) { $this->next->operate();} };}class ResponsibilityB extends Responsibility { public function __construct() {} public function operate(){if (false == is_null($this->next)) { $this->next->operate();} };} $res_a = new ResponsibilityA();$res_b = new ResponsibilityB();$res_a->setNext($res_b);?>
相关文章:
1. Springboot项目中运用vue+ElementUI+echarts前后端交互实现动态圆环图(推荐)2. php使用fputcsv实现大数据的导出操作详解3. python读取配置文件方式(ini、yaml、xml)4. vue实现简单图片上传5. Windows+Apache搭建PHP开发环境6. 浅谈Python数学建模之固定费用问题7. vs code创建并运行python的方法8. Springboot集成jdbcTemplate过程解析9. Android Studio使用Kotlin时,修改代码后运行不生效的解决方法10. python argparse模块通过后台传递参数实例

网公网安备