基本介紹
- 中介者模式(Mediator Pattern)炭分,用一個中介對象來封裝一系列的對象交互桃焕。中介者使各個對象不需要顯式地相互引用,從而使其耦合松散捧毛,而且可以獨立地改變它們之間的交互
- 中介者模式屬于行為型模式观堂,使代碼易于維護
- 比如MVC模式,C (Controller控制器)是M (Model模型)和V (View視圖)的中介者呀忧,在前后端交互時起到了中間人的作用
例子:
智能家庭項目:
1)智能家庭包括各種設(shè)備师痕,鬧鐘、咖啡機而账、電視機胰坟、窗簾等
2)主人要看電視時,各個設(shè)備可以協(xié)同工作泞辐,自動完成看電視的準(zhǔn)備工作笔横,比如流
程為:鬧鈴響起->咖啡機開始做咖啡->窗簾自動落下->電視機開始播放
實現(xiàn):
public abstract class Mediator {
public abstract void getMessage(int stateChange,String name);
public abstract void register(String name,Colleage colleage);
public abstract void sendMessage();
}
public abstract class Colleage {
private Mediator mediator;
public String name;
public Colleage(Mediator mediator, String name) {
super();
this.mediator = mediator;
this.name = name;
}
public class ConcereteMediator extends Mediator{
private HashMap<String, Colleage> map;
private HashMap<String, String> interMap;
public ConcereteMediator() {
// TODO Auto-generated constructor stub
map = new HashMap<>();
interMap = new HashMap<>();
}
@Override
public void getMessage(int stateChange,String name) {
// TODO Auto-generated method stub
Colleage colleage = map.get(name);
if (colleage instanceof Alarm) {
if (stateChange == 0) {
( (TV)(map.get(interMap.get("TV"))) ).play();
( (CoffeeMachine)(map.get(interMap.get("CoffeeMachine"))) ).play();
}else if (stateChange == 1) {
( (TV)(map.get(interMap.get("TV"))) ).stop();
( (CoffeeMachine)(map.get(interMap.get("CoffeeMachine"))) ).stop();
}
}else if(colleage instanceof TV){
}
}
public void register(String name,Colleage colleage) {
map.put(name, colleage);
if (colleage instanceof Alarm) {
interMap.put("Alarm", name);
}else if (colleage instanceof TV) {
interMap.put("TV", name);
}else if(colleage instanceof CoffeeMachine){
interMap.put("CoffeeMachine", name);
}
}
@Override
public void sendMessage() {
// TODO Auto-generated method stub
}
}
public Mediator getMediator(){
return this.mediator;
}
public abstract void sendMessage(int stateChange);
}
public class Alarm extends Colleage {
public Alarm(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.register(name, this);
}
@Override
public void sendMessage(int stateChange) {
// TODO Auto-generated method stub
this.getMediator().getMessage(stateChange,this.name);
}
@Override
public Mediator getMediator() {
// TODO Auto-generated method stub
return super.getMediator();
}
}
public class TV extends Colleage{
public TV(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.register(name, this);
}
@Override
public void sendMessage(int stateChange) {
// TODO Auto-generated method stub
this.getMediator().getMessage(stateChange,this.name);
}
@Override
public Mediator getMediator() {
// TODO Auto-generated method stub
return super.getMediator();
}
public void play() {
// TODO Auto-generated method stub
System.out.println("電視開始播放...");
}
public void stop() {
// TODO Auto-generated method stub
System.out.println("電視停止播放");
}
}
public class CoffeeMachine extends Colleage {
public CoffeeMachine(Mediator mediator, String name) {
super(mediator, name);
// TODO Auto-generated constructor stub
mediator.register(name, this);
}
@Override
public void sendMessage(int stateChange) {
// TODO Auto-generated method stub
this.getMediator().getMessage(stateChange,this.name);
}
@Override
public Mediator getMediator() {
// TODO Auto-generated method stub
return super.getMediator();
}
public void play() {
// TODO Auto-generated method stub
System.out.println("咖啡機開始工作...");
}
public void stop() {
// TODO Auto-generated method stub
System.out.println("咖啡機停止工作...");
}
}
public class Client {
public static void main(String[] args) {
Mediator mediator = new ConcereteMediator();
Alarm alarm = new Alarm(mediator, "Alarm");
TV tv = new TV(mediator, "Tv");
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "coffeMachine");
alarm.sendMessage(0);
System.out.println("----------------");
alarm.sendMessage(1);
}
}
中介者模式的注意事項和細節(jié)
- 多個類相互耦合,會形成網(wǎng)狀結(jié)構(gòu),使用中介者模式將網(wǎng)狀結(jié)構(gòu)分離為星型結(jié)構(gòu)咐吼,進行解耦
- 減少類間依賴狠裹,降低了耦合,符合迪米特原則
- 中介者承擔(dān)了較多的責(zé)任汽烦,一旦中介者出現(xiàn)了問題涛菠,整個系統(tǒng)就會受到影響
- 如果設(shè)計不當(dāng),中介者對象本身變得過于復(fù)雜,這點在實際使用時俗冻,要特別注意