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

<?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. centos7.5 搭建上FTP服务

    1.安装FTP # 查看ftp 是否安装 rpm -qa | grep vftpd # 安装vsftp yum install -y vsftpd # 设置ftp 开机启动 systemctl ena ...

  2. Kubernetes之Taints与Tolerations 污点和容忍

    NodeAffinity节点亲和性,是Pod上定义的一种属性,使Pod能够按我们的要求调度到某个Node上,而Taints则恰恰相反,它可以让Node拒绝运行Pod,甚至驱逐Pod. Taints(污 ...

  3. Redis NOAUTH Authentication required

    redis设置密码后停止服务报错,NOAUTH Authentication required 可以修改/etc/init.d/redis文件中的stop命令 $CLIEXEC -p $REDISPO ...

  4. 日志篇 随着win10更新...

    随着win10更新到最新..我发现电脑出了点状况.. 一 是电脑睡眠主机不关闭...然后用了 https://jingyan.baidu.com/article/cb5d6105d76343005c2 ...

  5. AQS1---走向稳定态

    AQS的思想(稳定思想):即使确定了正常节点,这个节点也可能下一秒异常,即使找到了正常节点,这个节点可能只是异常status=0/-1的节点,这些都不要紧,都只是在自己旋转‘生命周期’里面和自己所看到 ...

  6. vue的package.json文件理解

    参考文档: https://www.cnblogs.com/tzyy/p/5193811.html#_h1_0 https://www.cnblogs.com/hongdiandian/p/83210 ...

  7. 关于mpvue编写小程序的坑

    在引入第三方UI时 百度了很多方法,对于第一次接触的,在我们写vue的页面需要添加main.json 同时在写文件时,要在最后不要加 “,”  !!!并且在微信小程序会出现路径报错 在微信小程序中点击 ...

  8. mysql获取某个字段平均值方法AVG函数的使用

    直接上脚本 ,)) AS 平均分 FROM students WHERE sex= '男' 其中,特别说明一下CAST关键字 CAST(字段名 as 要转换的类型) #其中,可以转换的类型为: CHA ...

  9. 【拆分版】Docker-compose构建Kibana单实例,基于7.1.0

    写在前边 今凌晨的时候已经把这整个Docker-compose构建的ELK集群跑起来了,有点没熬住,所以早上起来补文档,今天就上到公司测试服务器上测试了,好开森. 本文内容就是红框的部分,只是启动个K ...

  10. FusionInsight大数据开发---SparkStreaming概述

    SparkStreaming概述 SparkStreaming是Spark核心API的一个扩展,它对实时流式数据的处理具有可扩展性.高吞吐量.可容错性等特点. SparkStreaming原理 Spa ...