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

减少了各对象之间的耦合,使得可以独立的改变或复用各个Mediator或Colleague
Mediator控制了交互,导致Mediator变的复杂

ps:

各个同事类需要知道中介者,但各个同事类并不知道彼此(各对象不需要显示的相互引用).

主要的交互逻辑由中介者完成

在同事类中添加接口来增加中介者,导致职责缺乏单一,除非是需要复杂的交互逻辑.

UML:

示例代码:

一个Colleague发消息,一个Colleague收消息,中介者处理交互.

// 抽象中介者
abstract class Mediator
{
protected $sender;
protected $receiver; public function setSender(Colleague $colleague)
{
$this->sender = $colleague;
} public function setReceiver(Colleague $colleague)
{
$this->receiver = $colleague;
} abstract public function sendMessage(Colleague $colleague, $message);
} // 具体中介者
class ConcreateMediator extends Mediator
{
public function sendMessage(Colleague $colleague, $message)
{
if ($colleague instanceof $this->sender) {
$this->receiver->receiveMessage($message);
}
}
} // 抽象同事
abstract class Colleague
{
protected $mediator; public function __construct(Mediator $mediator)
{
$this->mediator = $mediator;
}
} // 同事1
class Colleague1 extends Colleague
{
public function sendMessage($message)
{
$this->mediator->sendMessage($message, $this);
}
} // 同事2
class Colleague2 extends Colleague
{
public function receiveMessage($message)
{
echo "同事2收到消息{$message}";
}
} $mediator = new ConcreateMediator(); $colleague1 = new Colleague1($mediator);
$colleague2 = new Colleague2($mediator); $mediator->setSender($colleague1); // 命令一般由中介者发出
$mediator->setReceiver($colleague2);
$mediator->sendMessage($colleague1, '你好');

  

B8:中介者模式 Mediator的更多相关文章

  1. 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...

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

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

  3. 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...

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

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

  5. 中介者模式(Mediator Pattern)

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

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

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

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

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

  8. 中介者模式(Mediator)

    GOF:用一个中介对象来封装一系列的对象交互.中介者使对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 类图:

  9. 大熊君说说JS与设计模式之------中介者模式Mediator

    一,总体概要 1,笔者浅谈 我们从日常的生活中打个简单的比方,我们去房屋中介租房,房屋中介人在租房者和房东出租者之间形成一条中介.租房者并不关心他租谁的房.房东出租者也不关心他租给谁.因为有中介的存在 ...

随机推荐

  1. HUST-1350 Trie

    1350 - Trie 时间限制:1秒 内存限制:128兆 104 次提交 35 次通过 题目描述 In computer science, a trie, is an ordered tree da ...

  2. mysql绿色版配置

    MySQL 绿色版安装配置教程 一.下载,这里使用绿色免安装版 1.网上搜索mysql绿色免安装版下载即可.提供官网地址 http://dev.mysql.com/downloads/mysql--- ...

  3. Docker Ubuntu容器安装ping(zz)

    更新apt-get的软件包信息,然后再安装 sudo docker run ubuntu apt-get update sudo docker run ubuntu apt-get install i ...

  4. Laravel5.5 生成测试数据

    1.在database/factories/UserFactory.php 中添加 2.在tinker中生成数据 3.数据生成成功

  5. Jest+Enzyme React js/typescript测试环境配置案例

    本文案例github:https://github.com/axel10/react-jest-typescript-demo 配置jest的react测试环境时我们可以参考官方的配置教程: http ...

  6. CF 1005B Delete from the Left 【模拟数组操作/正难则反】

    You are given two strings s and t. In a single move, you can choose any of two strings and delete th ...

  7. dutacm.club 1094: 等差区间(RMQ区间最大、最小值,区间GCD)

    1094: 等差区间 Time Limit:5000/3000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total ...

  8. Scala实战高手****第14课:Scala集合上的函数式编程实战及Spark源码鉴赏

    package com.dt.spark.scala.bascis object Functional_Itearal {   def main(args: Array[String]): Unit ...

  9. linux的打包与解压

    zip: 打包 :zip something.zip something (目录请加 -r 参数) 解包:unzip something 指定路径:-d 参数 创建加密 zip 包 使用 -e 参数可 ...

  10. Ubuntu 16.04屏幕阅读Screen Reader导致快捷键失灵的问题解决

    开启和关闭快捷键:[Alt]+[Win]+[S] 如果关了之后开机还自动启动时,那么直接把它卸载: sudo apt-get remove gnome-orca killall orca 参考: ht ...