Catalogue of series articles
Design pattern - design principle
Create mode - singleton mode (I)
Creation mode - factory mode (II)
Creation mode - prototype mode (III)
Creative mode - builder mode (4)
Structural mode - adapter mode (I)
Structural mode - bridge mode (II)
Structural mode - decorator mode (III)
Structural mode - combined mode (IV)
Structural mode - appearance mode (V)
Structural mode - Xiangyuan mode (6)
Structural mode - agent mode (7)
Behavioral mode - template method mode (I)
Behavioral mode - command mode (II)
Behavioral model visitor model (III)
Behavioral mode iterator mode (4)
Behavioral model observer model (V)
Behavioral model - intermediary model (6)
Behavioral model - memorandum model (7)
Behavioral mode interpreter mode (8)
Behavioral mode - state mode (IX)
Behavioral model - Strategic Model (10)
Behavioral model - responsibility chain model (11)
preface
1, Intermediary model
1.1 introduction to intermediary model
-
Mediator mode:
- A mediation object is used to encapsulate the interaction of a series of objects. The mediator makes the objects do not need to show mutual references, reduces the chaotic dependencies between objects, so as to make their coupling loose, and can change their interaction independently;
- If there are many to many relationships between objects in a system, we can separate the interaction behavior between objects from each object, encapsulate it in a mediation object, and coordinate it uniformly;
- That is, the many to many network structure between objects becomes a star structure;
1.2 mediator mode structure
-
Abstract Mediator role:
- It is the interface of mediator and provides the abstract method of registering and forwarding colleague object information;
-
Concrete Mediator role:
- The specific mediation object implements the abstract method. It needs to know all the specific colleague classes, that is, it is managed as a collection, and accepts a colleague message to complete the response task;
-
Abstract Colleague role:
- Define the interface of the colleague class, save the mediator object, provide the abstract method of the interaction of the colleague object, and realize the common functions of all interacting colleague classes;
-
Concrete Colleague role:
- There are many specific colleagues. Each colleague only knows his own behavior, but does not know the behavior (method) of other colleagues. They all rely on the intermediary object;
2, Realize
example:
- A. if the pilot wants to take off, contact other pilots through the tower to see if they can take off;
2.1 intermediary realization
/** * Abstract mediator */ public abstract class TowerMediator { public abstract void registerColleague(CaptainColleague colleague); public abstract void sendMsg(CaptainColleague colleague, String msg); }
package com.dozezz.designpattern.mediator; import java.util.ArrayList; import java.util.List; /** * Specific intermediary */ public class ConcreteTowerMediator extends TowerMediator { List<CaptainColleague> captainColleagueList = new ArrayList<>(); @Override public void registerColleague(CaptainColleague colleague) { if(! captainColleagueList.contains(colleague)){ colleague.setTowerMediator(this); captainColleagueList.add(colleague); } } @Override public void sendMsg(CaptainColleague target, String msg) { for (CaptainColleague colleague : captainColleagueList) { if (colleague != target) { colleague.receiveMsg(msg); } } } }
package com.dozezz.designpattern.mediator; /** * Abstract colleague class */ public abstract class CaptainColleague { TowerMediator towerMediator; public void setTowerMediator(TowerMediator towerMediator) { this.towerMediator = towerMediator; } /** * send message */ public abstract void sendMsg(String msg); /** * receive messages */ public abstract void receiveMsg(String msg); }
package com.dozezz.designpattern.mediator; /** * Specific colleagues */ public class ACaptainConcreteColleague extends CaptainColleague { @Override public void sendMsg( String msg) { towerMediator.sendMsg(this, msg); } @Override public void receiveMsg(String msg) { System.out.println(String.format("%s The captain received the message: %s", "A", msg)); } }
package com.dozezz.designpattern.mediator; /** * Specific colleagues */ public class BCaptainConcreteColleague extends CaptainColleague { @Override public void sendMsg(String msg) { towerMediator.sendMsg(this, msg); } @Override public void receiveMsg(String msg) { System.out.println(String.format("%s The captain received the message: %s", "B", msg)); } }
package com.dozezz.designpattern.mediator; /** * Specific colleagues */ public class CCaptainConcreteColleague extends CaptainColleague { @Override public void sendMsg( String msg) { towerMediator.sendMsg(this, msg); } @Override public void receiveMsg(String msg) { System.out.println(String.format("%s The captain received the message: %s", "C", msg)); } }
package com.dozezz.designpattern.mediator; /** * Main test class */ public class ClientTest { public static void main(String[] args) { ConcreteTowerMediator concreteTowerMediator = new ConcreteTowerMediator(); ACaptainConcreteColleague colleague1 = new ACaptainConcreteColleague(); BCaptainConcreteColleague colleague2 = new BCaptainConcreteColleague(); CCaptainConcreteColleague colleague3 = new CCaptainConcreteColleague(); concreteTowerMediator.registerColleague(colleague1); concreteTowerMediator.registerColleague(colleague2); concreteTowerMediator.registerColleague(colleague3); colleague1.sendMsg("A Tell you: I am A,I'm taking off.. "); colleague2.sendMsg("B Tell you: I am B,Please don't take off. I'm still on the runway.."); colleague3.sendMsg("C Tell you: I am C,You can take off.."); } }
3, Summary of intermediary model
3.1 application scenarios of intermediaries
- There are complex references to objects in the system, which leads to confusion of dependency and structure and can not be reused;
- Want to encapsulate the behavior of multiple classes through an intermediate class, but don't want too many subclasses;
3.2 differences between mediator mode and observer mode
-
Observer mode is used for decoupling in one to many dependency scenarios. By introducing the observer role, the message publisher is decoupled from the specific subscriber;
-
The mediator decouples the complex many to many coupling relationship in the system, simplifies the network structure into a star structure and converts many to many into one to many;
-
They can realize the decoupling between the message publisher and the receiver, and the message publisher does not know the specific message receiver;
-
There is a similar relationship between the two patterns, which can replace transformation in some scenarios
- If the collaboration relationship is relatively simple, it can be implemented in the form of one to many, using the observer mode;
- If the collaboration relationship is more complex, you can use the mediator pattern;
3.3 difference between intermediary mode and appearance mode
- Both appearance pattern and mediator pattern reduce the coupling degree of the system through intermediate classes;
- Appearance mode is to provide a simple and consistent interface for the subsystem. The client sends messages to the subsystem through the facade. It can be considered that the message sending is unilateral;
- Skin mode between external clients and internal subsystems
- The mediator model is the cooperation among multiple objects in a complex system, and the mediator model is the cooperation among colleagues of various internal objects;
3.4 advantages and disadvantages of intermediary model
- It simplifies the relationship between objects, encapsulates the interaction between objects in the system, decouples the relationship between classes, and makes the system a low coupling system;
- The mediator model decouples all colleague objects, and it is more convenient to add new mediators or colleague classes;
- Because the mediator encapsulates the interaction between various classes, the complexity of the mediator class itself will increase, and the maintenance of the mediator class itself will become complex;
4, References
- http://c.biancheng.net/view/1390.html
- https://www.cnblogs.com/noteless/p/10178477.html
- https://www.bilibili.com/video/BV1G4411c7N4?p=54&spm_id_from=pageDriver