`
BlogDown
  • 浏览: 213736 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

12种行为模式 之8 MEDIATOR 调停者模式

阅读更多

8、MEDIATOR

调停者模式:

没使用调停者模式 : 一个过度耦合的系统 : 网状结构



使用调停者模式 后 : 以调停者为 中心的星状结构




/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 15:24:28
* <p/>
* note: 调停者角色
*/
abstract public class Mediator {

    /**
     * 事件方法
     * @param c
     */
    public abstract void colleagueChanged(Colleague c);

}

/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 15:24:39
* <p/>
* note: 具体调停者角色
*/
public class ConcreteMediator extends Mediator {
    private Colleague1 colleague1;
    private Colleague2 colleague2;

    /**
     * 将所有同事 实例化
     */
    public void createConcreteMediator() {
        colleague1 = new Colleague1(this);
        colleague2 = new Colleague2(this);
    }

    /**
     * 事件方法
     * @param c
     */
    public void colleagueChanged(Colleague c ) {
        colleague1.action();
        colleague2.action();
    }

    public Colleague1 getColleague1() {
        return colleague1 ;
    }

    public Colleague2 getColleague2() {
        return colleague2 ;
    }
}

/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 15:24:50
* <p/>
* note: 抽象同事角色
*       每个同事对象 仅知道调停者
*       而不知道其他同事对象
*/
public abstract class Colleague {
    private Mediator mediator;

    public Colleague(Mediator m) {
        mediator = m;
    }

    /**
     * 当前同时对象发生变化时,通知调停者
     */
    public void change() {
        mediator.colleagueChanged(this);
    }

    /**
     * 行动方法,一个同事对象在得知其他同事对象有变化时
     * 会执行此方法
     */
    public abstract void action();

    public Mediator getMediator() {
        return mediator;
    }
}

/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 15:25:00
* <p/>
* note: 具体同事角色
*/
public class Colleague1 extends Colleague {
    public Colleague1(Mediator m) {
        super(m);
    }

    public void action() {
        System.out.println("This is an action from Colleague 1");
    }
}

/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 15:25:10
* <p/>
* note: 具体同事角色
*/
public class Colleague2 extends Colleague {
    public Colleague2(Mediator m) {
        super( m );
    }

    public void action() {
        System.out.println("This is an action from Colleague 2");
    }
}

/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 15:25:20
* <p/>
* note: 具体同事角色
*/
public class Colleague3 extends Colleague {
    public Colleague3(Mediator m) {
        super( m );
    }

    public void action() {
        System.out.println("This is an action from Colleague 3");
    }
}

/**
* User: liuwentao@wentao365.com
* Date: 2008-12-9 Time: 15:34:30
* <p/>
* note: 客户端角色
*/
public class Client {

    public static void main(String args[]) {
        ConcreteMediator mediator = new ConcreteMediator();
        mediator.createConcreteMediator();

        Colleague c1 = new Colleague1(mediator);
        Colleague c2 = new Colleague2(mediator);

        mediator.colleagueChanged(c1);
    }
}


分享到:
评论

相关推荐

    (行为型模式) Mediator 中介者模式

    C#面向对象设计模式 (行为型模式) Mediator 中介者模式 视频讲座下载

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式

    设计模式之中介者模式(Mediator)

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

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式 (Level 300)

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式 (Level 300)

    C++ Mediator模式

    23种设计模式之十八(行为模式)Mediator模式

    JAVA设计模式之行为模式

    这是JAVA设计模式中属于行为模式的部分,包括Template(模板模式)、Chain of Responsibility(责任链模式)、Memento(纪念品模式)、Mediator(中介模式)、Strategy(策略模式)、State 、Observer(观察者模式)、Visitor...

    设计模式 Mediator 中介者 c++

    中介者模式的完整代码。 程序默认使用vs开发。其他开发工具可能需要做少许调整。

    设计模式之中介者模式(Mediator Pattern)

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

    mediator模式

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

    中介者模式(Mediator Pattern)原理图

    中介者模式(Mediator Pattern)是一种行为型设计模式,用于减少对象之间的直接相互依赖,使得对象间的交互通过一个中介者对象来进行协调。在中介者模式中,对象之间不再直接相互调用,而是通过中介者对象来传递消息...

    C++设计模式课件17_Mediator_中介者.pdf

    C++设计模式课件17_Mediator_中介者.pdf

    设计模式之行为模式(一)

    本资源是用VC6.0实现的行为模式,有八种:CommandPattern、MediatorPattern、MementoPattern、ObserverPattern、StatePattern、StrategyPattern、TemplatePattern、VisitorPattern。参考于《23种设计模式(C++).pdf》

    python实现常用的23种设计模式:含源代码、详细文档说明

    一、创建型模式 1、工厂方法模式【Factory Method】 2、抽象工厂模式【Abstract Factory】 ...8、调停者模式【Mediator】 9、备忘录模式【Memento】 10、迭代器模式【Iterator】 11、解释器模式【Interpreter】

    Mediator模式

    NULL 博文链接:https://gary0416.iteye.com/blog/913462

    JAVA设计模式chm文档

    创建模式: 设计模式之Factory 设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) 结构模式: ...设计模式之Mediator(中介者) 设计模式之Interpreter(解释器) 设计模式之Visitor

    32种设计模式

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23...

    用Java实现23种设计模式

    中介者模式(Mediator Pattern) 备忘录模式(Memento Pattern) 观察者模式(Observer Pattern) 状态模式(State Pattern) 空对象模式(Null Object Pattern) 策略模式(Strategy Pattern) 模板模式...

    设计模式精解-GoF 23种设计模式解析

    设计模式精解-GoF 23 种设计模式解析附 C++实现源码 目 录 引 言 0.1 设计模式解析(总序) 0.2 设计模式解析后记 0.3 与作者联系 1 创建型模式 1.1 Factory模式 1.2 AbstactFactory模式 1.3 Singleton...

    23种Python设计模式示例演示源码包.rar

    23种Python设计模式示例...Interpreter解释器模式,Iterator迭代器模式,Mediator中介者模式、Singleton光身汉模式,Observer观察者模式等,一共23种,这里不一一列举了,每一种模式代码,都包括了中文注解,利于学习。

    23种设计模式 (创建型,结构型,行为型)

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式...

Global site tag (gtag.js) - Google Analytics