中介者模式,又称调停者模式,用一个中介对象来封装一系列的对象交互。中介者使各个对象不需要显式地相互作用,从而使其耦合松散,并且可以独立地改变它们之间的交互。
中介者模式包含以下主要角色:
定义抽象中介者接口Mediator.java
public interface Mediator {
void register(String name, Colleague colleague);
void getMessage(int stateChange, String name);
void sendMessage();
}
定义具体中介者ConcreteMediator.java
public class ConcreteMediator implements Mediator {
private final Map colleagueMap;
private final Map interMap;
public ConcreteMediator() {
colleagueMap = new HashMap<>();
interMap = new HashMap<>();
}
@Override
public void register(String name, Colleague colleague) {
colleagueMap.put(name, colleague);
if (colleague instanceof Alarm) {
interMap.put("Alarm", name);
} else if (colleague instanceof CoffeeMachine) {
interMap.put("CoffeeMachine", name);
} else if (colleague instanceof TV) {
interMap.put("TV", name);
}
}
@Override
public void getMessage(int stateChange, String name) {
if (colleagueMap.get(name) instanceof Alarm) {
if (stateChange == 0) {
System.out.println("闹钟响了,开始煮咖啡!");
sendMessage(1);
} else if (stateChange == 1) {
System.out.println("休息完毕,开始工作!");
sendMessage(2);
}
} else if (colleagueMap.get(name) instanceof CoffeeMachine) {
System.out.println("啡机煮好了咖啡!");
sendMessage(3);
} else if (colleagueMap.get(name) instanceof TV) {
System.out.println("电视关闭了!");
}
}
@Override
public void sendMessage() {
for (String key : interMap.keySet()) {
String value = interMap.get(key);
if (key.equals("Alarm")) {
colleagueMap.get(value).sendMessage(0);
} else if (key.equals("CoffeeMachine")) {
colleagueMap.get(value).sendMessage(1);
} else if (key.equals("TV")) {
colleagueMap.get(value).sendMessage(0);
}
}
}
private void sendMessage(int stateChange) {
for (String key : interMap.keySet()) {
String value = interMap.get(key);
if (key.equals("Alarm")) {
colleagueMap.get(value).getMessage(stateChange);
} else if (key.equals("CoffeeMachine")) {
colleagueMap.get(value).getMessage(stateChange);
} else if (key.equals("TV")) {
colleagueMap.get(value).getMessage(stateChange);
}
}
}
}
定义抽象同事类Colleague.java
public abstract class Colleague {
protected final Mediator mediator;
protected String name;
public Colleague(Mediator mediator, String name) {
this.mediator = mediator;
this.name = name;
}
public abstract void sendMessage(int stateChange);
public abstract void getMessage(int stateChange);
}
定义具体同事类Alarm.java
public class Alarm extends Colleague {
public Alarm(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name, this);
}
public void send(int stateChange) {
sendMessage(stateChange);
}
@Override
public void sendMessage(int stateChange) {
this.mediator.getMessage(stateChange, name);
}
@Override
public void getMessage(int stateChange) {
System.out.println("闹钟发生改变,执行对应的操作!");
}
}
定义具体同事类CoffeeMachine.java
public class CoffeeMachine extends Colleague {
public CoffeeMachine(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name, this);
}
public void send(int stateChange) {
sendMessage(stateChange);
}
@Override
public void sendMessage(int stateChange) {
this.mediator.getMessage(stateChange, name);
}
@Override
public void getMessage(int stateChange) {
System.out.println("咖啡机发生改变,执行对应的操作!");
}
}
定义具体同事类TV.java
public class TV extends Colleague {
public TV(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name, this);
}
public void send(int stateChange) {
sendMessage(stateChange);
}
@Override
public void sendMessage(int stateChange) {
this.mediator.getMessage(stateChange, name);
}
@Override
public void getMessage(int stateChange) {
System.out.println("电视发生改变,执行对应的操作!");
}
}
客户端代码Client.java
public class Client {
public static void main(String[] args) {
Mediator mediator = new ConcreteMediator();
Alarm alarm = new Alarm(mediator, "Alarm");
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "CoffeeMachine");
TV tv = new TV(mediator, "TV");
alarm.send(0);
System.out.println();
alarm.send(1);
}
}
中介者模式是一种对象行为型模式,它能够将对象间的依赖关系转化为一种中介者和多个同事对象间的关联关系,从而实现抽象化,降低了对象之间的耦合度。通常来说,中介者模式适用于系统中对象之间复杂的引用关系和依赖关系结构,以及对象间产生的具体耦合关系难以满足需求的情况。