中介者模式


  • 概述 : 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互
  • 中介者模式所包含的角色
  • Mediator : 中介者角色.在里面定义各个部门之间的交互对象,
  • ConcreteMediator : 具体中介者实现角色.维护各个部门之间的交互关系
  • Deparement : 部门角色.所有具体部门类的父类.
  • ConreteDepaerment : 具体部门角色.实现与中介者角色的交互.从而实现与其他部门的交互
  • 使用场景
  • 系统中对象之间存在比较复杂的引用关系,导致他们之间的依赖关系结构混乱而已难以复用
  • 想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类
  • 优点
  • 降低了类的复杂度,将一对多的关系转化成了一对一(具体部门角色对中介者)
  • 各个类之间实现解耦
  • 符合迪米特原则
  • 缺点
  • 如果部门很多的话,中介类会非常庞大,难以维护





代码实现


        示例:一个公司,各个部门之间可能需要经常交互,这样非常的错综复杂.现在使用中介者模式来简单实现产品部门和研发部门的交互.现在产品部门通常要时刻了解研发部门的开发进度,所以两个部门需要经常进行联系.


   


抽象中介者角色 

package Mediator;

/**
 * Created by looper on 2017/8/28.
 */
public abstract class Mediator {
  public abstract void contact(Department department,String message);
}


具体中介者角色

package Mediator;

/**
 * Created by looper on 2017/8/28.
 */
public class ConcreteMediator extends Mediator {
  protected ResearchDepartment researchDepartment;
  protected ProductDepartment productDepartment;

  public ResearchDepartment getResearchDepartment() {
    return researchDepartment;
  }

  public void setResearchDepartment(ResearchDepartment researchDepartment) {
    this.researchDepartment = researchDepartment;
  }

  public ProductDepartment getProductDepartment() {
    return productDepartment;
  }

  public void setProductDepartment(ProductDepartment productDepartment) {
    this.productDepartment = productDepartment;
  }

  @Override
  public void contact(Department department, String message) {
    if ("研发部".equals(department.getName())) {
      productDepartment.getMessage(message);
    } else if ("产品部".equals(department.getName())) {
      researchDepartment.getMessage(message);
    }
  }
}


部门角色类

package Mediator;

/**
 * Created by looper on 2017/8/28.
 */
public abstract class Department {
  private String name;
  protected Mediator mediator;

  public Department(String name, Mediator mediator){
    this.name = name;
    this.mediator = mediator;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  //接收其他部门的消息
  public abstract void getMessage(String message);

}


具体部门角色1(产品部)

package Mediator;

/**
 * Created by looper on 2017/8/28.
 */
public class ProductDepartment extends Department {
  public ProductDepartment(String name, Mediator mediator) {
    super(name, mediator);
  }

  //接收其他部门的消息
  @Override
  public void getMessage(String message) {
    System.out.println("产品部刚刚收到消息 : "+message);
  }

  //向其他部门发送信息
  public void sendMessage(ProductDepartment productDepartment,String message) {
    mediator.contact(productDepartment,message);
  }

}


具体部门角色2(研发部)


package Mediator;

/**
 * Created by looper on 2017/8/28.
 */
public class ResearchDepartment extends Department {
  public ResearchDepartment(String name, Mediator mediator) {
    super(name, mediator);
  }

  //接受其他部门的消息
  @Override
  public void getMessage(String message) {
    System.out.println("研发部刚刚收到消息 : "+message);
  }

  //向其他部门发送消息
  public void sendMessage(ResearchDepartment researchDepartment, String message) {
    mediator.contact(researchDepartment,message);
  }

}


测试类

package Mediator;

/**
 * Created by looper on 2017/8/28.
 */
public class MediatorTest {
  public static void main(String[] args) {
    ConcreteMediator mediator = new ConcreteMediator();

    ProductDepartment productDepartment = new ProductDepartment("产品部",mediator);
    ResearchDepartment researchDepartment = new ResearchDepartment("研发部",mediator);

    mediator.setProductDepartment(productDepartment);
    mediator.setResearchDepartment(researchDepartment);

    productDepartment.sendMessage(productDepartment,"我是产品部,需要研发部提供具体的开发日程");
    researchDepartment.sendMessage(researchDepartment,"我的研发部,最新的开发日程已经发送到你们,请注意查收");
  }
}