二十三天学会设计模式之中介模式

487 阅读4分钟

你好,我是小黄,一名独角兽企业的Java开发工程师。 校招收获数十个offer,年薪均20W~40W。 感谢茫茫人海中我们能够相遇, 俗话说:当你的才华和能力,不足以支撑你的梦想的时候,请静下心来学习, 希望优秀的你可以和我一起学习,一起努力,实现属于自己的梦想。 欢迎关注我的公众号:“爱敲代码的小黄”

一、什么是中介者模式

名如其称,中介者模式类似我们在租房子的中介,中间人的意思。

在Java中有 MVC 架构, C(Controller控制)是V(View视图)、M(Model模型)的中介者,在前后端的相互中起到了中间人的作用。

中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。

中介者模式的结构图如下所示: 在这里插入图片描述

  • Mediator:中介者的抽象类
  • ConcreatMediator:中介者的实现类
  • Colleague:员工的抽象类
  • ConcreateColleague:员工的实现类

员工之间互相不认识,只能通过中介者(Mediator)发送信息,这就是所谓的中介者模式

二、为什么要用中介者模式

我们来看下面这个例子:

在联合国中,每个国家都可以通过联合国进行交流,维持世界和平。

当联合国不存在时,会发生什么情况呢?

在这里插入图片描述 每个国家想要与其他的国家通信,都要重新建立一条通信线,这种关系过于复杂。

如果以后又加入其他的国家,那么这种关系更加难以维护,所以我们可以采取中介者模式,结构如下: 在这里插入图片描述 每个人都可以通过我们的中介联系到对方,当有别的国家介入的时候,只需要将其关联至中介,即可关联所有人。

三、联合国代码的实现

总体架构如下所示: 在这里插入图片描述

  • Country
public class Country {
    public UnitedNations unitedNations;
	// 国家接入的中介者
    public Country(UnitedNations unitedNations) {
        this.unitedNations = unitedNations;
    }
}
  • USA
public class USA extends Country {
    public USA(UnitedNations unitedNations) {
        super(unitedNations);
    }
	// 向某个国家发送信息
    public void declare(String message, Country country) {
        unitedNations.declare(message, this, country);
    }
	// 接受某个国家的信息
    public void getMessage(String message, Country country) {
        System.out.println("美国获得" + country + "发来的信息:" + message);
    }

    @Override
    public String toString() {
        return "美国";
    }
}
  • Iraq
public class Iraq extends Country {

    public Iraq(UnitedNations unitedNations) {
        super(unitedNations);
    }

    public void declare(String message, Country country) {
        unitedNations.declare(message, this, country);
    }

    public void getMessage(String message, Country country) {
        System.out.println("伊拉克获得" + country.toString() + "发来的信息:" + message);
    }

    @Override
    public String toString() {
        return "伊拉克";
    }
}

  • UnitedNations
public abstract class UnitedNations {
    // 传递的消息 发送的国家 接受的国家
    public abstract void declare(String message, Country sendcountry, Country acceptCountry);
}

  • UnitedNationsSecurityCouncil
public class UnitedNationsSecurityCouncil extends UnitedNations {
    public USA usa;
    public Iraq iraq;

    public void setUsa(USA usa) {
        this.usa = usa;
    }


    public void setIraq(Iraq iraq) {
        this.iraq = iraq;
    }

    @Override
    public void declare(String message, Country sendcountry, Country acceptCountry) {
        // 接收方为伊拉克
        if (sendcountry == usa && acceptCountry == iraq) {
            iraq.getMessage(message, sendcountry);
        } else {
        	// 接收方为美国
            usa.getMessage(message, sendcountry);
        }
    }
}
  • 测试类
public class Main {
    public static void main(String[] args) {
        // 创建中介者
        UnitedNationsSecurityCouncil unitedNationsSecurityCouncil = new UnitedNationsSecurityCouncil();

        // 每个国家都接入中介者
        USA usa = new USA(unitedNationsSecurityCouncil);
        Iraq iraq = new Iraq(unitedNationsSecurityCouncil);

        // 中介者接入每个国家
        unitedNationsSecurityCouncil.setIraq(iraq);
        unitedNationsSecurityCouncil.setUsa(usa);

        // 国家发送消息
        usa.declare("美国坚决不使用核武器", iraq);
        iraq.declare("伊拉克坚决不使用核武器", usa);

		// 伊拉克获得美国发来的信息:美国坚决不使用核武器
		// 美国获得伊拉克发来的信息:伊拉克坚决不使用核武器 
		
    }
}

四、总结

我们可以看到,利用中介者模式可以很好的解耦各大国家之间的关系。

俗话说,有利必有弊,中介者模式的弊端就是:所有的国家都依赖中介,这个中介里面的逻辑特别的复杂。

我们在以下场景中会用到中介者模式:

  • 机场的飞机调度策略、房屋出租
  • MVC架构
  • 联合国/WTO作为中介者协调各个国家

所以,当我们系统中对象之间存在比较复杂的引用关系,想通过一个中间类来封装多个类的行为,而又不想生成太多的子类,那么就可以使用中介者模式。

本期的内容就到这里,我是一名独角兽企业的Java开发工程师,有问题可以留言或者私信加我微信,我们下期再见!