中介者模式
标签:设计模式

中介者模式(Mediator)

中介者模式是行为模式之一,在中介者模式中,类之间的交互行为被统一放在中介者的对象中,对象通过中介者对象同其他对象交互,中介者对象起着控制器的作用。

1. 中介者模式中的角色及其职责

1.1 Mediator

中介者类的抽象父类

AbstractMediator

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/04
 */
public abstract class AbstractMediator {
    private Man man;
    private Woman woman;


    public void setMan(Man man) {
        this.man = man;
    }

    public void setWoman(Woman woman) {
        this.woman = woman;
    }

    public Man getMan() {
        return man;
    }

    public Woman getWoman() {
        return woman;
    }

    public abstract void getPartner(Person person);
}

1.2 Concrete Mediator

具体的中介者类

Mediator

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/04
 */
public class Mediator extends AbstractMediator{

    @Override
    public void getPartner(Person person){
        if (person instanceof Man){
            this.setMan((Man) person);
        }else {
            this.setWoman((Woman) person);
        }

        if (super.getMan() == null || super.getWoman() == null){
            System.out.println("性别相同");
        }else {
            if (super.getMan().getAge() == super.getWoman().getAge()){
                System.out.println(super.getMan().getName() + " 和 " + super.getWoman().getName() + " 相配");
            }else {
                System.out.println(super.getMan().getName() + " 和 " + super.getWoman().getName() + " 不相配");
            }
        }
    }
}

1.3 Colleague

关联类的抽象父类

Person

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/04
 */
public abstract class Person {
    private String name;
    private int age;
    private Mediator mediator;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Mediator getMediator() {
        return mediator;
    }

    public void setMediator(Mediator mediator) {
        this.mediator = mediator;
    }

    public abstract void getPartner(Person person);
}

1.4 Concrete Colleague

具体的关联类

Man

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/04
 */
public class Man extends Person {

    public Man(String name, int age, Mediator mediator) {
        super(name, age, mediator);
    }

    @Override
    public void getPartner(Person person) {
        super.getMediator().setMan(this);
        super.getMediator().getPartner(person);
    }
}

Woman

package com.liuyao;

/**
 * @author liuyao
 * @date 2018/08/04
 */
public class Woman extends Person{

    public Woman(String name, int age, Mediator mediator) {
        super(name, age, mediator);
    }

    @Override
    public void getPartner(Person person) {
        super.getMediator().setWoman(this);
        super.getMediator().getPartner(person);
    }
}

Main

package com.liuyao;

public class Main {

    public static void main(String[] args) {
        Mediator mediator=new Mediator();
        Person zhangsan =new Man("张三",25,mediator);
        Person lisi =new Man("李四",24,mediator);
        Person xiaofang=new Woman("小芳",25,mediator);
        Person xiaoding=new Woman("小丁",23,mediator);

        zhangsan.getPartner(lisi);
        zhangsan.getPartner(xiaofang);
        zhangsan.getPartner(xiaoding);
    }
}

//性别相同
//张三 和 小芳 相配
//张三 和 小丁 不相配

2. 中介者模式的优点

  1. 将系统按功能分割成更小的对象
  2. 对管理对象的集中控制
  3. 减小类的耦合程度
  4. 有利于提高类的重用性
  • 3 min read

CONTRIBUTORS


  • 3 min read