13、仲裁者模式(Mediator Pattern)

1. 仲裁者模式

1.1 简介

  仲裁者模式又称为调停者模式或中介者模式,主要是仲裁和中介的作用,帮助其它类之间进行通信,降低多个对象和类之间的通信复杂性。主要包括仲裁者(Mediator)和组员(Colleague),组员的动作需要会向仲裁者汇报,仲裁者会根据全局的实际情况向其他Colleague作出指示,共同完成一定的逻辑功能。仲裁者通常处理不同类之间的通信,并支持松耦合,使代码易于维护。作为行为型的模式之一,应用场景有限并不被大家熟知。

1.2 仲裁者结构

仲裁者uml:

仲裁者uml.jpg

仲裁者成员:

  • 抽象仲裁者(mediator):定义一个接口用于和对象通信(SmartDevice)
  • 具体仲裁者(concretemediator):协调各同事对象实现协作,了解维护各个同事()
  • 抽象同事角色(colleague):规定了同事的基本类型
  • 具体同事角色(concreteColleague):每个同事都知道仲裁者对象,要与同事通信则把通信告诉仲裁者

2. 示例

  我们以房屋出租为例,房东通过中介将房子租给租客,直接从中介处拿到租金,租客通过中介找到房东进行维修,同时物业通过中介向房东收取物业费。在此例中,中介相当于仲裁者或者中介者,房东、租客和物业就是组员,组员之间通过中介进行通信,实现收租、维修、收物业费等动作。

仲裁者接口类:

public interface Mediator {

    void collectRents(int money);
    void repair();
    void collectPropertyFee(int money);
}

房屋中介:

public class HouseMediator implements Mediator {

    private Person houseRenter;
    private Person renter;
    private Person property;

    public HouseMediator(Person houseRenter, Person renter,
        Person property) {
        this.houseRenter = houseRenter;
        this.renter = renter;
        this.property = property;
    }

    @Override
    public void collectRents(int money) {

    }

    public void repair() {

    }

    @Override
    public void collectPropertyFee(int money) {

    }

    public Person getHouseRenter() {
        return houseRenter;
    }

    public void setHouseRenter(Person houseRenter) {
        this.houseRenter = houseRenter;
    }

    public Person getRenter() {
        return renter;
    }

    public void setRenter(Person renter) {
        this.renter = renter;
    }

    public Person getProperty() {
        return property;
    }

    public void setProperty(Person property) {
        this.property = property;
    }
}

组员虚拟类:

public abstract class Person {

    private Mediator houseMediator;

    public abstract void action();

    public Mediator getHouseMediator() {
        return houseMediator;
    }

    public void setHouseMediator(Mediator houseMediator) {
        this.houseMediator = houseMediator;
    }
}

房东:

public class HouseOwner extends Person {

    @Override
    public void action() {
        this.getHouseMediator().collectRents(1000);
    }
}

租客:

public class Renter extends Person {

    @Override
    public void action() {
        this.getHouseMediator().repair();
    }
}

物业:

public class Property extends Person {

    @Override
    public void action() {
        this.getHouseMediator().collectPropertyFee(300);
    }
}

客户端调用:

    public static void main(String[] args) {

        Person houseRenter = new HouseOwner();
        Person renter = new Renter();
        Person property = new Property();

        Mediator mediator = new HouseMediator(houseRenter, renter, property);
        houseRenter.setHouseMediator(mediator);
        renter.setHouseMediator(mediator);
        property.setHouseMediator(mediator);

        houseRenter.action();
        renter.action();
        property.action();
    }

3. 总结

仲裁者模式的优点:

  • 降低了类的复杂度,将一对多转化成了一对一。
  • 各个类之间的解耦,减少对象之间的关联性,让每一个对象都能够独立
  • 符合迪米特原则。
  • 不会引入太多其他的系统
  • 系统被依赖的程度降低

仲裁者模式的缺点:

  • 中介者会庞大,变得复杂难以维护。
  • 如果游戏中有大量的功能模块需要中介者,那么担任中介者的角色类将会因为担任大量的中介者角色而容易产生操作接口爆炸的情况。为避免这种情况,我们可以搭配其他设计模式使用。

仲裁者模式的使用场景:

  • 系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象。
  • 想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。
  • 机场调度系统。
  • MVC 框架,其中C(控制器)就是 M(模型)和 V(视图)的中介者。
  • 数据库引擎:内部可以分成数个子系统,有专门负责数据库连接地功能与产生数据库操作语句地功能,两个子功能之间地沟通可以通过中介者模式(Mediator)来进行,让两者之间不相互依赖,方便抽换另一个子系统
  • 游戏内各系统的整合
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。