Java设计模式 - 中介者模式

1.定义

用一个中介对象来封装一系列的对象交互,该中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

2. 角色

  • Mediator:抽象中介者
  • ConcreteMediator:具体中介者
  • Colleague:抽象同事类
  • ConcreteColleague:具体同事类

Java设计模式 - 中介者模式_第1张图片

3. 特点

  • 优点:可以减少对象之间的交互,使对象解耦,将控制逻辑集中,可以简化系统的维护。
  • 缺点:因为中介者包含了具体同事之间的交互,若设计不当,可能导致具体中介者类十分复杂。

4. 示例

Mediator:

public abstract class Mediator {

    public Map map;

    public Mediator(){
        map = new HashMap<>();
    }

    public void register(String user, Colleague colleague) {
        map.put(user, colleague);
    }

    public abstract void operation(String receiver, String msg);
}

ConcreteMediator:

public class ConcreteMediator extends Mediator {

    @Override
    public void operation(String receiver, String msg) {
        Colleague colleague = map.get(receiver);
        if (colleague != null) {
            colleague.receiveMsg(msg);
        } else {
            System.out.println(receiver + " is not exist");
        }
    }
}

Colleague:

public abstract class Colleague {

    public Mediator mediator;

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

    public abstract void receiveMsg(String msg);

    public abstract void sendMsg(String receiver, String msg);
}

具体同事类:

// ConcreteColleagueA
public class ConcreteColleagueA extends Colleague {
    @Override
    public void receiveMsg(String msg) {
        System.out.println("Colleague A receives a message");
        System.out.println(msg);
    }

    @Override
    public void sendMsg(String receiver, String msg) {
        System.out.println("Colleague A sends a message");
        mediator.operation(receiver, msg);
    }
}


// ConcreteColleagueB
public class ConcreteColleagueB extends Colleague {

    @Override
    public void receiveMsg(String msg) {
        System.out.println("Colleague B receives a message");
        System.out.println(msg);
    }

    @Override
    public void sendMsg(String receiver, String msg) {
        System.out.println("Colleague B sends a message");
        mediator.operation(receiver, msg);
    }
}

测试类:

public class TestMediator {

    public static void main(String[] args) {

        Mediator mediator = new ConcreteMediator();
        Colleague colleagueA = new ConcreteColleagueA();
        Colleague colleagueB = new ConcreteColleagueB();
        colleagueA.setMediator(mediator);
        colleagueB.setMediator(mediator);
        mediator.register("A", colleagueA);
        mediator.register("B", colleagueB);

        colleagueA.sendMsg("B","the message is from colleague A");
        System.out.println("--------------------------------------------------");
        colleagueB.sendMsg("A", "the message is from colleague B");
        System.out.println("--------------------------------------------------");
        colleagueA.sendMsg("C", "the message is from colleague A");
    }
}


// 输出
// Colleague A sends a message
// Colleague B receives a message
// the message is from colleague A
// --------------------------------------------------
// Colleague B sends a message
// Colleague A receives a message
// the message is from colleague B
// --------------------------------------------------
// Colleague A sends a message
// C is not exist

 

参考:

1. 《Head First 设计模式》

2. 《图说设计模式》https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/mediator.html

你可能感兴趣的:(设计模式)