定义一个中介对象来简化原有对象之间的交互关系,降低系统中对象间的耦合度,使原有对象之间不必相互了解。
角色:
- IMediator:抽象中介者,定义了同事对象到中介者对象的接口
- AbstractColleague:抽象同时类
- ConcreteMediator:具体中介者,管理所有同事类,并在接收到某个同事对象消息时,完成相应的任务
- ConcreteColleague:具体同事类,每个同事只知道自己的行为,而不了解其他同事的行为,但是他们都依赖中介者对象
package com.strife.pattern.mediator;
import java.util.HashMap;
import java.util.Map;
/**
* 中介者模式
*
* @author mengzhenghao
* @date 2022/6/4
*/
public class Mediator {
public static void main(String[] args) {
IMediator mediator = new ConcreteMediator();
AbstractColleague alarm = new Alarm("alarm", mediator);
AbstractColleague coffeeMachine = new CoffeeMachine("coffeeMachine", mediator);
AbstractColleague tv = new TV("TV", mediator);
AbstractColleague curtains = new Curtains("Curains", mediator);
alarm.sendMsg(0);
System.out.println("-------------------------");
alarm.sendMsg(1);
}
}
/** 抽象中介者 */
interface IMediator {
/** 将给中介者对象,加入到集合中 */
public abstract void register(String colleagueName, AbstractColleague colleague);
/** 接收消息, 具体的同事对象发出 */
public abstract void getMessage(int stateChange, String colleagueName);
public abstract void sendMessage();
}
/** 抽象同事类 */
abstract class AbstractColleague {
protected String name;
/** 持有中介者 */
private IMediator mediator;
public AbstractColleague(String name, IMediator mediator) {
this.name = name;
this.mediator = mediator;
}
public IMediator getMediator() {return mediator;}
public abstract void sendMsg(int stateChange);
}
/** 具体中介者 */
class ConcreteMediator implements IMediator {
private Map<String, AbstractColleague> colleagueMap;
private Map<String, String> interMap;
public ConcreteMediator() {
colleagueMap = new HashMap<>();
interMap = new HashMap<>();
}
@Override
public void register(String colleagueName, AbstractColleague colleague) {
colleagueMap.put(colleagueName, colleague);
if (colleague instanceof Alarm) {
interMap.put("Alarm", colleagueName);
} else if (colleague instanceof CoffeeMachine) {
interMap.put("CoffeeMachine", colleagueName);
} else if (colleague instanceof TV) {
interMap.put("TV", colleagueName);
} else if (colleague instanceof Curtains) {
interMap.put("Curtains", colleagueName);
}
}
@Override
public void getMessage(int stateChange, String colleagueName) {
//处理闹钟发出的消息
if (colleagueMap.get(colleagueName) instanceof Alarm) {
if (stateChange == 0) {
((CoffeeMachine) colleagueMap.get(interMap.get("CoffeeMachine"))).StartCoffee();
((TV) colleagueMap.get(interMap.get("TV"))).StartTv();
} else if (stateChange == 1) {
((CoffeeMachine) colleagueMap.get(interMap.get("CoffeeMachine"))).FinishCoffee();
((TV) colleagueMap.get(interMap.get("TV"))).StopTv();
}
} else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
((Curtains) colleagueMap.get(interMap.get("Curtains"))).UpCurtains();
} else if (colleagueMap.get(colleagueName) instanceof TV) {
}
}
@Override
public void sendMessage() {
}
}
/** 具体同事类 */
class Alarm extends AbstractColleague {
public Alarm(String name, IMediator mediator) {
super(name, mediator);
//注册到中介者
mediator.register(name, this);
}
public void SendAlarm(int stateChange) {
sendMsg(stateChange);
}
@Override
public void sendMsg(int stateChange) {
getMediator().getMessage(stateChange, this.name);
}
}
class CoffeeMachine extends AbstractColleague {
public CoffeeMachine(String name, IMediator mediator) {
super(name, mediator);
//注册到中介者
mediator.register(name, this);
}
@Override
public void sendMsg(int stateChange) {
getMediator().getMessage(stateChange, this.name);
}
public void StartCoffee() {
System.out.println("It's time to startcoffee!");
}
public void FinishCoffee() {
System.out.println("After 5 minutes!");
System.out.println("Coffee is ok!");
sendMsg(0);
}
}
class TV extends AbstractColleague {
public TV(String name, IMediator mediator) {
super(name, mediator);
//注册到中介者
mediator.register(name, this);
}
@Override
public void sendMsg(int stateChange) {
getMediator().getMessage(stateChange, this.name);
}
public void StartTv() {
System.out.println("It's time to StartTv!");
}
public void StopTv() {
System.out.println("StopTv!");
}
}
class Curtains extends AbstractColleague {
public Curtains(String name, IMediator mediator) {
super(name, mediator);
mediator.register(name, this);
}
@Override
public void sendMsg(int stateChange) {
getMediator().getMessage(stateChange, this.name);
}
public void UpCurtains() {
System.out.println("I am holding Up Curtains!");
}
}