第六章 7.1 中介者模式

分类:01_设计模式

标签:

7.1.1 中介者模式介绍

提到中介模式,有一个比较经典的例子就是航空管制。 为了让飞机在飞行的时候互不干扰,每架飞机都需要知道其他飞机每时每刻的位置,这就需要时刻跟其他飞机通信。飞机通信形成的通信网络就会无比复杂。这个时候,我们通过引 入“塔台”这样一个中介,让每架飞机只跟塔台来通信,发送自己的位置给塔台,由塔台来 负责每架飞机的航线调度。这样就大大简化了通信网络。

129.jpg

中介模式(mediator pattern)的定义: 定义一个单独的(中介)对象,来封装一组对象之间的交互,将这组对象之间的交互委派给予中介对象交互,来避免对象之间的交互.

中介者对象就是用于处理对象与对象之间的直接交互,封装了多个对象之间的交互细节。中介模式的设计跟中间层很像,通过引入中介这个中间层,将一组对象之间的交互关系从多对多的网状关系转换为一对多的星状关系.原来一个对象要跟N个对象交互,现在只需要跟一个中介对象交互,从而最小化对象之间的交互关系,降低代码的复杂度,提高代码的可读性和可维护性.

7.1.2 中介者模式原理

132.jpg

中介者模式包含以下主要角色:

7.1.3 中介者模式实现

代码示例


/**
* 抽象中介者
* @author spikeCong
* @date 2022/10/20
**/
public interface Mediator {

   void apply(String key);
}

/**
* 具体中介者
* @author spikeCong
* @date 2022/10/20
**/
public class MediatorImpl implements Mediator {

   @Override
   public void apply(String key) {
       System.out.println("最终中介者执行操作,key为: " + key);
   }
}

/**
* 抽象同事类
* @author spikeCong
* @date 2022/10/20
**/
public abstract class Colleague {

   private Mediator mediator;

   public Colleague(Mediator mediator) {
       this.mediator = mediator;
   }

   public Mediator getMediator() {
       return mediator;
   }

   public abstract void exec(String key);
}

/**
* 具体同事类
* @author spikeCong
* @date 2022/10/20
**/
public class ConcreteColleagueA extends Colleague {

   public ConcreteColleagueA(Mediator mediator) {
       super(mediator);
   }

   @Override
   public void exec(String key) {
       System.out.println("====在组件A中,通过中介者执行!");
       getMediator().apply(key);
   }
}

public class ConcreteColleagueB extends Colleague {

   public ConcreteColleagueB(Mediator mediator) {
       super(mediator);
   }

   @Override
   public void exec(String key) {
       System.out.println("====在组件B中,通过中介者执行!");
       getMediator().apply(key);
   }
}

public class Client {

   public static void main(String[] args) {

       //创建中介者
       MediatorImpl mediator = new MediatorImpl();

       //创建同事对象
       Colleague c1 = new ConcreteColleagueA(mediator);
       c1.exec("key-A");
       Colleague c2 = new ConcreteColleagueB(mediator);
       c2.exec("key-B");
   }
}


====在组件A中,通过中介者执行!
最终中介者执行操作,key为: key-A
====在组件B中,通过中介者执行!
最终中介者执行操作,key为: key-B

7.1.4 中介者模式应用实例

【例】租房

现在租房基本都是通过房屋中介,房主将房屋托管给房屋中介,而租房者从房屋中介获取房屋信息。房屋中介充当租房者与房屋所有者之间的中介者。


/**
* 抽象中介者
* @author spikeCong
* @date 2022/10/20
**/
public abstract class Mediator {

   //申明一个联络方法
   public abstract void contact(String message,Person person);
}

/**
* 抽象同事类
* @author spikeCong
* @date 2022/10/20
**/
public abstract class Person {

   protected String name;

   protected Mediator mediator;

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

/**
* 中介机构
* @author spikeCong
* @date 2022/10/20
**/
public class MediatorStructure extends Mediator {

   //中介要知晓房主和租房者的信息
   private HouseOwner houseOwner;
   private Tenant tenant;

   public HouseOwner getHouseOwner() {
       return houseOwner;
   }

   public void setHouseOwner(HouseOwner houseOwner) {
       this.houseOwner = houseOwner;
   }

   public Tenant getTenant() {
       return tenant;
   }

   public void setTenant(Tenant tenant) {
       this.tenant = tenant;
   }

   @Override
   public void contact(String message, Person person) {
       if(person == houseOwner){  //如果是房主,则租房者获得信息
           tenant.getMessage(message);
       }else { //如果是租房者则获取房主信息
           houseOwner.getMessage(message);
       }
   }
}

/**
* 具体同事类-房屋拥有者
* @author spikeCong
* @date 2022/10/20
**/
public class HouseOwner extends Person{

   public HouseOwner(String name, Mediator mediator) {
       super(name, mediator);
   }

   //与中介者联系
   public void contact(String message){
       mediator.contact(message,this);
   }

   //获取信息
   public void getMessage(String message){
       System.out.println("房主" + name + "获取到的信息: " + message);
   }
}

/**
* 具体同事类-承租人
* @author spikeCong
* @date 2022/10/20
**/
public class Tenant extends Person{

   public Tenant(String name, Mediator mediator) {
       super(name, mediator);
   }

   //与中介者联系
   public void contact(String message){
       mediator.contact(message,this);
   }

   //获取信息
   public void getMessage(String message){
       System.out.println("租房者" + name + "获取到的信息: " + message);
   }
}

public class Client {

   public static void main(String[] args) {

       //一个房主 一个租房者 一个中介机构
       MediatorStructure mediator = new MediatorStructure();

       //房主和租房者只需要知道中介机构即可
       HouseOwner houseOwner = new HouseOwner("路飞", mediator);
       Tenant tenant = new Tenant("娜美", mediator);

       //中介收集房租和租房者信息
       mediator.setHouseOwner(houseOwner);
       mediator.setTenant(tenant);

       tenant.contact("需要一个两室一厅的房子,一家人住");
       houseOwner.contact("出租一套两室一厅带电梯,月租5000");
   }
}

7.1.5 中介者模式总结

1) 中介者模式的优点

2) 中介者模式的缺点

3) 中介者模式使用场景


修改内容