中介者(Mediator)

定义

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

UML图

角色

  Mediator:中介者接口。在里面定义了各个同事之间相互交互所需要的方法,可以是公共的方法,如Change方法,也可以是小范围的交互方法。

  ConcreteMediator:具体的中介者实现对象。它需要了解并为维护每个同事对象,并负责具体的协调各个同事对象的交互关系。

  Colleague:同事类的定义,通常实现成为抽象类,主要负责约束同事对象的类型,并实现一些具体同事类之间的公共功能,比如,每个具体同事类都应该知道中介者对象,也就是每个同事对象都会持有中介者对象的引用,这个功能可定义在这个类中。

  ConcreteColleague:具体的同事类,实现自己的业务,需要与其他同事对象交互时,就通知中介对象,中介对象会负责后续的交互。

例子

  联合国机构类 相当于Mediator类

package com.csdhsm.pattemdesign.mediator;

/**  
 * @Title:  UnitedNations.java   
 * @Description: 联合国机构类 相当于Mediator类
 * @author: Han   
 * @date:   2016年6月30日 下午7:46:59  
 */  
public abstract class UnitedNations {
    
    public abstract void declare(String message, Country colleague);
}

  国家类 相当于Colleague类

package com.csdhsm.pattemdesign.mediator;

/**  
 * @Title:  Country.java   
 * @Description: 国家类 相当于Colleague类
 * @author: Han   
 * @date:   2016年6月30日 下午7:48:51  
 */  
public abstract class Country {
    
    protected UnitedNations mediator;
    
    public Country(UnitedNations mediator) {
        this.mediator = mediator;
    }
}

  美国类  相当于ConcreteColleague1类

package com.csdhsm.pattemdesign.mediator;

/**  
 * @Title:  USA.java   
 * @Description: 美国类  相当于ConcreteColleague1类
 * @author: Han   
 * @date:   2016年6月30日 下午7:49:20  
 */  
public class USA extends Country {
    
    public USA(UnitedNations mediator) {
        super(mediator);
    }
    
    public void declare(String message) {
        mediator.declare(message, this);
    }
    
    public void getMessage(String message) {
        System.out.println("美国获得对方信息: " + message);
    }
}

  伊拉克类  相当于ConcreteColleague2类

package com.csdhsm.pattemdesign.mediator;

/**  
 * @Title:  Iraq.java   
 * @Description: 伊拉克类  相当于ConcreteColleague2类
 * @author: Han   
 * @date:   2016年6月30日 下午7:49:33  
 */  
public class Iraq extends Country {

    public Iraq(UnitedNations mediator) {
        super(mediator);
    }
    
    public void declare(String message) {
        mediator.declare(message, this);
    }
    
    public void getMessage(String message) {
        System.out.println("伊拉克获得对方信息: " + message);
    }
}

  联合国安理会 相当于ConcreteMediator类

package com.csdhsm.pattemdesign.mediator;

/**  
 * @Title:  UnitedNationsSecurityCouncil.java   
 * @Description: 联合国安理会 相当于ConcreteMediator类
 * @author: Han   
 * @date:   2016年6月30日 下午7:50:13  
 */  
public class UnitedNationsSecurityCouncil extends UnitedNations {

    private USA colleague1;
    private Iraq colleague2;
    
    public void setColleague1(USA colleague1) {
        this.colleague1 = colleague1;
    }

    public void setColleague2(Iraq colleague2) {
        this.colleague2 = colleague2;
    }

    @Override
    public void declare(String message, Country colleague) {
        if(colleague == colleague1) {
            colleague2.getMessage(message);
        } else {
            colleague1.getMessage(message);
        }
    }
}

  客户端

package com.csdhsm.pattemdesign.mediator;

public class Solution {
    public static void main(String[] args) {
        
        UnitedNationsSecurityCouncil UNSC = new UnitedNationsSecurityCouncil();
        USA c1 = new USA(UNSC);
        Iraq c2 = new Iraq(UNSC);
        UNSC.setColleague1(c1);
        UNSC.setColleague2(c2);
        
        c1.declare("不准研制核武器,否则要发动战争!");
        c2.declare("我们没有核武器,也不怕侵略。");
    }
}

  结果

  OK,成功。

总结

  使用终结者模式的场合

    1.一组定义良好的对象,现在要进行复杂的通信。

    2.定制一个分布在多个类中的行为,而又不想生成太多的子类。

    可以看出,中介对象主要是用来封装行为的,行为的参与者就是那些对象,但是通过中介者,这些对象不用相互知道。呵呵~~~

  使用中介者模式的优点:

    1.降低了系统对象之间的耦合性,使得对象易于独立的被复用。

    2.提高系统的灵活性,使得系统易于扩展和维护。

  使用中介者模式的缺点:

    中介者模式的缺点是显而易见的,因为这个“中介“承担了较多的责任,所以一旦这个中介对象出现了问题,那么整个系统就会受到重大的影响。

 posted on 2016-06-30 19:57  韩思明  阅读(170)  评论(0编辑  收藏  举报