設計模式學習(十八)中介者模式

程式胖發表於2020-11-02

中介者模式

前言

  • 首先通過一個案例來演示:
    在這裡插入圖片描述

  • 傳統模式來解決的話,類圖就是這樣的:
    在這裡插入圖片描述

  • 問題分析:

    • 當各電器物件有多種狀態改變時,相互之間的呼叫關係會比較複雜
    • 各個電器物件彼此聯絡你中有我,我中有你,不利於鬆耦合
    • 各個電器物件之間所傳遞的訊息引數 ,容易混亂
    • 當系統增加一個新的電器物件時,或者執行流程改變時,程式碼的可維護性、擴充套件性都不理想
  • 解決方法:使用中介者模式

基本介紹

  • 中介者模式用一箇中介物件來封裝一系列的物件互動。
  • 中介者使各個物件不需要顯式地相互引用,從而使其耦合鬆散,而且可以獨立地改變它們之間的互動
  • 中介者模式屬於行為型模 式, 使程式碼易於維護
  • 類圖:
    在這裡插入圖片描述

應用實現

  • 類圖:
    在這裡插入圖片描述
  • 程式碼:
//中介者介面
public abstract class Mediator {
	//將給中介者物件,加入到集合中
	public abstract void Register(String colleagueName, Colleague colleague);

	//接收訊息, 具體的同事物件發出
	public abstract void GetMessage(int stateChange, String colleagueName);

	public abstract void SendMessage();
}
//具體的中介者類
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);
		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

	}

}
//同事抽象類
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);
}
//咖啡機
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);
	}
}
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!");
	}
}
//具體的同事類
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);
	}

}
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!");
	}

}
public class ClientTest {

	public static void main(String[] args) {
		//建立一箇中介者物件
		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);
	}

}

  • 結果:
    在這裡插入圖片描述

總結

  • 多個類相互耦合 ,會形成網狀結構 , 使用中介者模式 將網狀結構分離為星型結構進行解耦
  • 減少類間依賴,降低了耦合符合迪米特原則
  • 中介者承擔了較多的責任,一旦中介者出現了問題,整個系統就會受到影響
  • 如果設計不當,中介者物件本身變得過於復 雜, 這點在實際使用時,要特別注意

相關文章