PHP设计模式之调解者模式的深入解析
发布时间:2020-12-13 06:22:40 所属栏目:PHP教程 来源:网络整理
导读:调解者模式,这个模式的目的是封装一组对象之间的相互作用,防止对象之间相互干扰,调解者(Mediator)在同事对象(Colleague)之间充当中间汇聚点。同事对象之间应该保持松散耦合,避免一个对象直接明确指向另一个对象。在调解者模式下,对象的关系和依赖发
调解者模式,这个模式的目的是封装一组对象之间的相互作用,防止对象之间相互干扰,调解者(Mediator)在同事对象(Colleague)之间充当中间汇聚点。同事对象之间应该保持松散耦合,避免一个对象直接明确指向另一个对象。在调解者模式下,对象的关系和依赖发生冲突时,我们可以使用调解者在耦合的对象之间协调工作流,依赖可以从同事朝调解者或从调解者向同事建立,这两个方向上的依赖都可以使用AbstractColleague或AbstractMediator中断。 ◆调解者(Mediator):协同多个Colleagues(AbstractColleagues)共同工作。 ◆AbstractMediator,AbstractColleague:从这些角色的真实实现解耦的可选接口,可能不止一个AbstractColleague角色。 下面的代码实现了一个表单输入的过滤过程,类似于Zend_Form_Element功能。 代码如下: /** * AbstractColleague. */ interface Filter { public function filter($value); } /** * Colleague. We decide in the implementation phase * that Colleagues should not know the next Colleague * in the chain,resorting to a Mediator to link them together. * This choice succesfully avoids a base abstract class * for Filters. * Remember that this is an example: it is not only * Chain of Responsibility that can be alternatively implemented * as a Mediator. */ class TrimFilter implements Filter { public function filter($value) { return trim($value); } } * Colleague. * The Mediator. We avoid referencing it from ConcreteColleagues (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |