Intermediary model
Basic introduction
- Mediator Pattern, which encapsulates a series of object interactions with a mediation object. Mediators make objects do not need to explicitly refer to each other, so that they are loosely coupled, and their interaction can be changed independently
- The mediator pattern is a behavioral pattern, which makes the code easy to maintain
- For example, in MVC mode, C (Controller) is the mediator of M (Model model) and V (View), which acts as an intermediary in front and back-end interaction
Schematic class diagram of mediator pattern
Description of the schematic class diagram - i.e. (roles and responsibilities of the mediator pattern)
- Mediator is an abstract mediator, which defines the interface from colleague object to mediator object
- Collague is an abstract Colleague class
- ConcreteMediator is a concrete mediator object that implements abstract methods. It needs to know all the specific colleague classes, that is, it is managed in a collection
HashMap, and accept a colleague object message to complete the corresponding task
There are many concrete colleague classes. Each colleague only knows his own behavior and does not know the behavior (method) of other colleague classes, but they all rely on the mediator object
package com.zhangqi.day1.mediator; /** * @author: zhangqi * @create: 2022/2/4 18:26 */ public class Client { public static void main(String[] args) { //Create a mediator object Mediator mediator = new ConcreteMediator(); //Create Alarm and add to HashMap of ConcreteMediator object Alarm alarm = new Alarm(mediator, "alarm"); //The CoffeeMachine object is created and And add to HashMap of ConcreteMediator object CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "coffeeMachine"); //Create Curtains and And add to HashMap of ConcreteMediator object Curtains curtains = new Curtains(mediator, "curtains"); TV tV = new TV(mediator, "TV"); //Let the alarm clock send a message alarm.SendAlarm(0); coffeeMachine.FinishCoffee(); alarm.SendAlarm(1); } }
package com.zhangqi.day1.mediator; /** * Colleague abstract class * * @author: zhangqi * @create: 2022/2/4 18:19 */ 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.zhangqi.day1.mediator; /** * @author: zhangqi * @create: 2022/2/4 18:20 */ public abstract class Mediator { //Add the object to the mediator to the collection public abstract void Register(String colleagueName, Colleague colleague); //Receive the message and send it to the specific colleague object public abstract void GetMessage(int stateChange, String colleagueName); public abstract void SendMessage(); }
package com.zhangqi.day1.mediator; /** * @author: zhangqi * @create: 2022/2/4 18:23 */ public class Curtains extends Colleague { public Curtains(Mediator mediator, String name) { super(mediator, name); } @Override public void SendMessage(int stateChange) { } public void UpCurtains() { System.out.println("I am holding Up Curtains!"); } }
package com.zhangqi.day1.mediator; /** * @author: zhangqi * @create: 2022/2/4 18:21 */ public class TV extends Colleague { public TV(Mediator mediator, String name) { super(mediator, name); mediator.Register(name, this); } @Override public void SendMessage(int stateChange) { } public void StartTv() { } public void StopTv() { } }
package com.zhangqi.day1.mediator; /** * @author: zhangqi * @create: 2022/2/4 18:25 */ public class Alarm extends Colleague { //constructor public Alarm(Mediator mediator, String name) { super(mediator, name); //When creating the Alarm colleague object, put yourself into the ConcreteMediator object [collection] mediator.Register(name, this); } public void SendAlarm(int stateChange) { SendMessage(stateChange); } @Override public void SendMessage(int stateChange) { // getMessage of the called mediator object this.GetMediator().GetMessage(stateChange, this.name); } }
package com.zhangqi.day1.mediator; import java.util.HashMap; /** * @author: zhangqi * @create: 2022/2/4 18:22 */ public class ConcreteMediator extends Mediator { //Set and put all colleague objects private HashMap<String, Colleague> colleagueMap; private HashMap<String, String> interMap; public ConcreteMediator() { colleagueMap = new HashMap<>(); interMap = new HashMap<>(); } @Override public void Register(String colleagueName, Colleague colleague) { colleagueMap.put(colleagueName, colleague); interMap.put(colleague.getClass().getSimpleName(), colleagueName); } // 1. Complete the corresponding task according to the received message // 2. In this way, the intermediary coordinates each specific colleague object to complete the task @Override public void GetMessage(int stateChange, String colleagueName) { //Process messages sent by alarm clock 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) {//If TV finds a message } else if (colleagueMap.get(colleagueName) instanceof Curtains) { //If it's a message from a curtain, deal with it here } } @Override public void SendMessage() { } }
package com.zhangqi.day1.mediator; /** * @author: zhangqi * @create: 2022/2/4 18:24 */ public class CoffeeMachine extends Colleague { public CoffeeMachine(Mediator mediator, String name) { super(mediator, name); } @Override public void SendMessage(int stateChange) { } 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); } }
Considerations and details of the mediator model
- When multiple classes are coupled with each other, a network structure will be formed. The intermediary mode is used to separate the network structure into a star structure for decoupling
- Reduce the dependence between classes and reduce the coupling, which is in line with the Demeter principle
- Intermediaries bear more responsibilities. Once the intermediaries have problems, the whole system will be affected
If not designed properly, the mediator object itself becomes too complex, which should be paid special attention to in practical use