Illustration intermediary model Java Design Patterns

Smart Home project

1) includes a variety of smart home devices, alarm clock, coffee machine, TV, curtains, etc.
2) when the owner to watch TV, individual devices can work together, watch TV automatically preparations, such as process: the alarm sounds - " coffee machine starts to make coffee - "automatic curtain fall -" TV start playing

Traditional solutions to smart home management

Here Insert Picture Description
1) When each object with multiple electrical state changes, call the mutual relations would be more complicated.
2) the object of each electrical contact with each other, you have me, I have you, is not conducive to loosely coupled.
3) message (parameter) electrical passed between the various objects, easily lead to confusion
4) When the system adds a new electrical object, or when performing a change of flow, code maintainability, extensibility is not ideal.

Basic introduction intermediary model

1) The intermediary model (Mediator Pattern), with an intermediary object that encapsulates set of objects interact.
Mediator the respective objects do not explicitly referring to each other, so that it loosely coupled, and can be changed independently of the interaction between them.
2) intermediary model belongs to the type of model, making the code easier to maintain.
3) such as MVC pattern, C (Controller Controller) is M (Model model) and V (View view) of the intermediary, intermediaries to play a role when the rear end of the front interact.

Principle of intermediary model class diagram

Here Insert Picture Description
Class diagram illustrative of the principles of:
1) Mediator Mediator abstract is to define an interface class colleagues object to the intermediary object.
2) Colleague colleague is an abstract class.
3) ConcreteMediator specific intermediary object implements the abstract method, he needs to know all of the specific type of co-workers, that is a set of management HashMap, and receives a colleague object message, to accomplish the task.
4) ConcreteColleague specific colleagues class, there will be many, each of my colleagues only know their own behavior, rather than to understand the behavior (methods) like other colleagues, but they are dependent on an intermediary object.

Intermediary model - the smart family of operating procedures

Here Insert Picture Description

  1. Objects created ConcreMediator
  2. Create a colleague each class object, such as: Alarm, CoffeeMachine, TV
  3. When you create an object of class colleagues, directly by the constructor, added to colleagueMap
  4. Colleagues class object, you can call sendMessage, eventually to call the getMessage method ConcreteMediator
  5. getMessage will call other colleagues to coordinate objects based on object receives a message colleagues issued to complete the task
  6. GetMessage can see is the core method, your task is completed
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);
	}

}

Notes and details of the intermediary model

1) a plurality of classes coupled to each other, will form a site structure, the mesh structure intermediary model is separated into the star structure, decoupled.
2) reduce the dependency between two classes, class of coupling reduced, Demeter compliance.
3) mediator responsible for the class more, once the mediator there is a problem, the whole system will be affected.
4) If poorly designed, Mediator object itself becomes too complicated, this is in actual use, should pay special attention.

Guess you like

Origin www.cnblogs.com/haizai/p/12622618.html