中介者模式


中介者模式


  • 中介者模式定义用一个中介对象封装一系列对象交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变他们之间的交互(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)。

  • 中介者模式的通用类图如下:

    • Mediator抽象中介者角色抽象中介者角色定义统一的接口,用于各同事角色之间通信
    • ConcreteMediator具体中介者角色通过协调同事角色实现协作行为,因此必须依赖各个同事角色
    • Colleague抽象同事类指定中介者
    • ConcreteColleague具体同事类每个同事都知道中介者角色,而且在与其他的同事进行通信时,一定通过中介者角色的协调。每个同事类的角色分为两种,一种是处理同时本身的行为,比如改变对象本身的状态;一种是必须依赖中介者才能完成的行为,叫做依赖方法
    • public abstract class Mediator {
          //具体的同事类
          protected ConcreteColleague1 colleague1;
          protected ConcreteColleague2 colleague2;
          public void setColleague1(ConcreteColleague1 colleague1){
              this.colleague1 = colleague1;
          }
          public void setColleague2(ConcreteColleague2 colleague2){
              this.colleague2 = colleague2;
          }
          //中介者模式的业务逻辑
          public abstract void doSomething1();
          public abstract void doSomething2();
      }
      
      public class ConcreteMediator extends Mediator{
      
          @Override
          public void doSomething1() {
              // TODO Auto-generated method stub
              super.colleague2.selfMethod();
          }
          @Override
          public void doSomething2() {
              // TODO Auto-generated method stub
              super.colleague1.selfMethod();
          }
      }
      
      public abstract class Colleague {
          protected Mediator mediator;
          public Colleague(Mediator mediator){
              this.mediator = mediator;
          }
      }
      
      public class ConcreteColleague1 extends Colleague{
          public ConcreteColleague1(Mediator mediator) {
              super(mediator);
              mediator.setColleague1(this);
              // TODO Auto-generated constructor stub
          }
          public void selfMethod(){
              //该类自己的业务逻辑
              System.out.println("This is a self-method");
          }
          public void depMethod(){
              //该类与其它类有关联的业务逻辑
              super.mediator.doSomething1();
          }
      }
      
      public class ConcreteColleague2 extends Colleague{
      
          public ConcreteColleague2(Mediator mediator) {
              super(mediator);
              mediator.setColleague2(this);
              // TODO Auto-generated constructor stub
          }
          public void selfMethod(){
              System.out.println("This is a self method in colleague2");
          }
          public void depMethod(){
              System.out.println("This is a dependent method in colleague2");
              super.mediator.doSomething2();
          }
      }
      
      public class Client {
      
          /**
           * @param args
           */
          public static void main(String[] args) {
              // TODO Auto-generated method stub
              Mediator mediator = new ConcreteMediator();
              ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
              ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);
              colleague1.depMethod();
              colleague2.depMethod();
          }
      }

       

  • 中介者模式的优点

中介者模式的优点是减少类间依赖,将原有的一对多的关系转化为一对一的依赖关系,同事类只依赖中介者,降低了类间的耦合。

  • 中介者模式的缺点

中介者模式的缺点是中介者会随着业务逻辑的增加而迅速膨胀,而且逻辑复杂,原本N个对象直接的相互依赖关系转化为中介者和同事类的依赖关系,同事类越多,中介者的逻辑越复杂。

posted @ 2016-11-15 20:53  best.lei  阅读(349)  评论(0编辑  收藏  举报