1)智能家庭包括各類設備,鬧鐘、咖啡機、電視機、窗簾等
2)主人要看電視時,各個設備能夠協同工做,自動完成看電視的準備工做,好比流程爲 :鬧鈴響起 - 》咖啡機開始作咖啡 -》窗簾自動落下 -》電視機開始播放java
1)當各電器對象有多種狀態改變時,相互之間的調用關係會比較複雜。
2)各個電器對象彼此聯繫,你中有我,我中有你,不利於鬆耦合。
3)各個電器對象之間所傳遞的消息(參數),容易混亂
4)當系統增長一個新的電器對象時,或者執行流程改變時,代碼的可維護性、擴展性都不理想。後端
1)中介者模式(Mediator Pattern),用一箇中介對象來封裝一系列的對象交互。
中介者使各個對象不須要顯式地相互引用,從而使其耦合鬆散,並且能夠獨立地改變它們之間的交互。
2)中介者模式屬於行爲型模式,使代碼易於維護。
3)好比MVC模式,C(Controller控制器)是M(Model模型)和V(View視圖)的中介者,在先後端交互時起到來中間人的做用。設計模式
對原理類圖的說明 :
1)Mediator 就是抽象中介者,定義類同事對象到中介者對象的接口。
2)Colleague 是抽象同事類。
3)ConcreteMediator 具體的中介者對象,實現抽象方法,他須要知道全部的具體的同事類,即以一個集合來管理HashMap,並接收某個同事對象消息,完成相應的任務。
4)ConcreteColleague 具體的同事類,會有不少,每一個同事只知道本身的行爲,而不瞭解其餘同事類的行爲(方法),可是他們都是依賴中介者對象。ide
package com.example.demo.mediator; public abstract class Mediator { //將給中介者對象,加入到集合中 public abstract void Register(String colleagueName, Colleague colleague); //接收消息, 具體的同事對象發出 public abstract void GetMessage(int stateChange, String colleagueName); public abstract void SendMessage(); } package com.example.demo.mediator; public abstract class Colleague { private Mediator mediator; public String name; public Colleague(Mediator mediator, String name) { this.mediator = mediator; this.name = name; } public Mediator GetMediator() { return this.mediator; } public abstract void SendMessage(int stateChange); } package com.example.demo.mediator; public class Alarm extends Colleague { public Alarm(Mediator mediator, String name) { super(mediator, name); // TODO Auto-generated constructor stub //在建立 Alarm 同事對象時,將本身放入到 ConcreteMediator 對象中[集合] mediator.Register(name, this); } public void SendAlarm(int stateChange) { SendMessage(stateChange); } @Override public void SendMessage(int stateChange) { // TODO Auto-generated method stub //調用的中介者對象的 getMessage this.GetMediator().GetMessage(stateChange, this.name); } } package com.example.demo.mediator; public class CoffeeMachine extends Colleague { 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); } 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!"); SendMessage(0); } } package com.example.demo.mediator; public class Curtains extends Colleague { public Curtains(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); } public void UpCurtains() { System.out.println("I am holding Up Curtains!"); } } package com.example.demo.mediator; public class TV extends Colleague { 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); } public void StartTv() { // TODO Auto-generated method stub System.out.println("It's time to StartTv!"); } public void StopTv() { // TODO Auto-generated method stub System.out.println("StopTv!"); } } package com.example.demo.mediator; import java.util.HashMap; public class ConcreteMediator extends Mediator { //集合,放入全部的同事對象 private HashMap<String, Colleague> colleagueMap; private HashMap<String, String> interMap; public ConcreteMediator() { colleagueMap = new HashMap<String, Colleague>(); interMap = new HashMap<String, String>(); } @Override public void Register(String colleagueName, Colleague colleague) { colleagueMap.put(colleagueName, colleague); // TODO Auto-generated method stub 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); } } //具體中介者的核心方法 //1. 根據獲得消息,完成對應任務 //2. 中介者在這個方法,協調各個具體的同事對象,完成任務 @Override public void GetMessage(int stateChange, String colleagueName) { // TODO Auto-generated method stub //處理鬧鐘發出的消息 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) { ((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) { //若是 TV 發現消息 } else if (colleagueMap.get(colleagueName) instanceof Curtains) { //若是是以窗簾發出的消息,這裏處理... } } @Override public void SendMessage() { // TODO Auto-generated method stub } } package com.example.demo.mediator; import java.util.HashMap; public class Client { public static void main(String[] args) { // TODO Auto-generated method stub //建立一箇中介者對象 Mediator mediator = new ConcreteMediator(); //建立 Alarm 而且加入到 ConcreteMediator 對象的 HashMap Alarm alarm = new Alarm(mediator, "alarm"); //建立了 CoffeeMachine 對象,並 且加入到 ConcreteMediator 對象的 HashMap CoffeeMachine coffeeMachine = new CoffeeMachine(mediator,"coffeeMachine"); //建立 Curtains , 並 且加入到 ConcreteMediator 對象的 HashMap Curtains curtains = new Curtains(mediator, "curtains"); TV tV = new TV(mediator, "TV"); //讓鬧鐘發出消息 alarm.SendAlarm(0); coffeeMachine.FinishCoffee(); alarm.SendAlarm(1); } }
1)多個類相互耦合,會造成網站結構,使用中介者模式將網狀結構分離爲星型結構,進行解耦。
2)減小類間依賴,下降類耦合,符合迪米特法則。
3)中介者承擔類較多的責任,一旦中介者出現了問題,整個系統就會受到影響。
4)若是設計不當,中介者對象自己變得過於複雜,這點在實際使用時,要特別注意。網站