差不多了,睡一觉,下次再弄。

<?php
/*
The more classes we have in our software, the more complex their communication
becomes. The mediator pattern addresses this complexity by encapsulating it into
a mediator object. Objects no longer communicate directly, but rather through a
mediator object, therefore lowering the overall coupling.
*/

interface MediatorInterface {
	public function fight();
	public function talk();
	public function registerA(ColleagueA $a);
	public function registerB(ColleagueB $b);
}

class ConcreteMediator implements MediatorInterface {
	protected $talk;
	protected $fight;

	public function registerA(ColleagueA $a) {
		$this->talk = $a;
	}

	public function registerB(ColleagueB $b) {
		$this->fight = $b;
	}

	public function fight() {
		echo 'fighting...<br/>';
	}

	public function talk() {
		echo 'talking...<br/>';
	}
}

abstract class Colleague {
	protected $mediator;
	public abstract function doSomething();
}

class ColleagueA extends Colleague {
	public function __construct(MediatorInterface $mediator) {
		$this->mediator = $mediator;
		$this->mediator->registerA($this);
	}

	public function doSomething() {
		$this->mediator->talk();
	}
}

class ColleagueB extends Colleague {
	public function __construct(MediatorInterface $mediator) {
		$this->mediator = $mediator;
		$this->mediator->registerB($this);
	}

	public function doSomething() {
		$this->mediator->fight();
	}
}

$mediator = new ConcreteMediator();
$talkColleague = new ColleagueA($mediator);
$fightColleague = new ColleagueB($mediator);

$talkColleague->doSomething();
$fightColleague->doSomething();
?>

  输出:

talking...
fighting...

  

php调停者模式(mediator pattern)的更多相关文章

  1. 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  2. 二十四种设计模式:中介者模式(Mediator Pattern)

    中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...

  3. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  4. 《Java设计模式》之调停者模式(Mediator)

    调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从而使它们能够较松散地耦合.当这些对象中的某些对象之间的相互作用发生改变时,不会马上影响到其它的一些 ...

  5. 中介者模式(Mediator Pattern)

    用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...

  6. 调停者模式Mediator(中介者模式)详解

    原文链接:https://www.cnblogs.com/java-my-life/archive/2012/06/20/2554024.html 在阎宏博士的<JAVA与模式>一书中开头 ...

  7. 18.中介者模式(Mediator Pattern)

    using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...

  8. [设计模式] 17 中介者模式 Mediator Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ...

  9. 4.7 《硬啃设计模式》 第24章 麻烦的多角关系 - 中介者模式(Mediator Pattern)简介

    在Windows程序中,有时候界面控件之间的交互会很麻烦,如:A控件显示什么的时候,B控件要显示什么,另外C控件要不可用,同样其它控件也会有类似的复杂要求.控件与控件之间很容易形成复杂的多角关系了.现 ...

随机推荐

  1. 【Gamma阶段】第五次Scrum Meeting

    [Gamma阶段]第五次Scrum Meeting 每日任务内容 今日工作任务 明日待完成任务 配合前端调整评论页面的样式 课程列表页针对移动端进行调整 戴荣 Gamma阶段后测试点样例编写 移除部分 ...

  2. 记一次node节点异常排查

    一.背景 公司进行kubernetes服务重启演练,集群重启后所有服务正常,仅node2节点处于notready状态 二.排查过程 查看对应节点的详细信息,发现kubelet一直处于starting状 ...

  3. Python3+PyCryptodome实现各种加密算法教程

    一.说明 PyCryptodome是python一个强大的加密算法库,可以实现常见的单向加密.对称加密.非对称加密和流加密算法.直接pip安装即可: pip install pycryptodome ...

  4. package.json中dependencies 与devDependencies 的区别

    首先,dependencies中安装的依赖是生产环境的依赖,即项目要运行所必须安装的包:devDependencies中安装的的依赖是开放环境的依赖,即在开发项目时需要安装的依赖. 其次,在安装依赖的 ...

  5. [转帖]什么是IOC(控制反转)、DI(依赖注入)

    什么是IOC(控制反转).DI(依赖注入) 2018-08-22 21:29:13 Ming339456 阅读数 20642   原文地址(摘要了部分内容):https://blog.csdn.net ...

  6. AntDesign vue学习笔记(八)Table服务端分页使用

    本文是AntDesign后端分页方法 1.设置pagination <a-table :columns="columns" :dataSource="data&qu ...

  7. SpringCloud Stream使用案例

    官方定义 Spring Cloud Stream 是一个构建消息驱动微服务的框架. 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互 ...

  8. react 中 Modal 多次使用且带参数不同实现

    一.举例:对于 echatrs 的柱子分别需要弹窗 带参数 触发弹窗出现事件 showModalhref myChart.on('click', (params) => { switch (pa ...

  9. Prometheus 监控linux服务器

    Prometheus 监控linux服务器 node_exporter:用于*NIX系统监控,使用Go语言编写的收集器. 使用版本 node_exporter 0.17.0 相关文档 使用文档:htt ...

  10. Nginx入门教程(转)

    原文:https://www.cnblogs.com/qdhxhz/p/8910174.html nginx入门教程 一.概述    什么是nginx?   Nginx (engine x) 是一款轻 ...