现在的位置: 首页 > 综合 > 正文

设计模式22:Mediator Pattern (调解者模式)

2013年09月11日 ⁄ 综合 ⁄ 共 5363字 ⁄ 字号 评论关闭
文章目录

英文原文:http://www.dofactory.com/Patterns/PatternMediator.aspx

一、Mediator Pattern (调解者模式)的定义

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

定义一个对象将一系列对象如何相互作用的操作进行封装。调解者通过避免一个对象明确指向其余的对象来促使自由耦合,同时能够让你单独的改变他们之间的相互作用。

二、UML类图

Median

  • Mediator  (IChatroom)
    • 定义一个接口用于与同事级对象进行通信
  • ConcreteMediator  (Chatroom)
    • 通过与同事级对象进行对等来实现相互协作行为
    • 知道并且维持本身的同事级对象
  • Colleague classes  (Participant)
    • 每个同事级对象都知道自己的调节者对象
    • 每个同事级对象在与另外的同事级对象进行通信时必须要与调节者进行通信。

    三、调解者模式实例代码

    四、另外一个使用调解者模式的实例代码

    This real-world code demonstrates the Mediator pattern facilitating loosely coupled communication between different Participants registering with a Chatroom. The Chatroom is the central hub through which all communication takes place. At this point only one-to-one communication is implemented in the Chatroom, but would be trivial to change to one-to-many.

    抱歉!评论已关闭.