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

<?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. postgresql小计

    1. postgresql执行结束后,判断结果是否成功,有几种结果 typedef enum { PGRES_EMPTY_QUERY = 0, /* empty query string was ex ...

  2. 【技术博客】nginx服务器的https协议实现

    在本学期软件工程的Alpha和Beta阶段,我们的服务器部署都是使用基础的http协议,http在网络路由间的信息转发都为明文,这对我们网站的账户密码登录来说很不安全,因此在Gamma阶段我们实现了h ...

  3. how to compile and replace ubuntu kernel

    how to compile and replace ubuntu kernel 0. environment -ubuntu 1804 64bit 1. prepare source code su ...

  4. Swagger2.9.2进入API界面报NumberFormatException异常

    [转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11778377.html     作者:jstarseven    码字挺辛苦的.....   ...

  5. Linux常用基础(一)

    1.命令解释器 shell---Unix操作系统 bash---Linux操作系统 本质:根据输入的命令,调用相应的执行程序. 2.Linux下的快捷键 (1)命令和路径补全 Tab键 (2)主键盘的 ...

  6. 『保卫王国 树上倍增dp』

    保卫王国 Description Z 国有n座城市,n - 1条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻扎军队.驻扎军队需 ...

  7. 【spring boot】【redis】spring boot基于redis的LUA脚本 实现分布式锁

    spring boot基于redis的LUA脚本 实现分布式锁[都是基于redis单点下] 一.spring boot 1.5.X 基于redis 的 lua脚本实现分布式锁 1.pom.xml &l ...

  8. 在 Docker 中运行 SpringBoot 应用

    创建 SpringBoot 项目 用 Idea 创建一个 SpringBoot 项目,编写一个接口: package cloud.dockerdemo import org.springframewo ...

  9. c#调用python脚本实现排序(适用于python脚本中不包含第三方模块的情况)

    引用:https://www.cnblogs.com/zoe-yan/p/10374757.html 利用vs2017c#调用python脚本需要安装IronPython.我是通过vs2017的工具- ...

  10. 网络编程中用到的SOCKET是什么?

    摘取网络上的一些内容: 什么是TCP/IP.UDP? Socket在哪里呢? Socket是什么呢? 什么是TCP/IP.UDP? TCP/IP(Transmission Control Protoc ...