Objective
The intermediary pattern can be thought of by listening to the name is also a design mode to solve the coupling degree, in fact, the intermediary pattern is very similar to the observer and the command pattern in structure, and the application purpose is some similarity with the structure pattern "façade pattern". But the difference from the command pattern is that most of the mediator roles are transparent to the client program. Of course, the reason for this difference is that they have to achieve different purposes.
Introduction to the concept of intermediary mode
The mediator pattern refers to the use of a mediation object to encapsulate a series of object interactions. Intermediaries make objects that do not need to be displayed in mutual references, so that they are loosely coupled, and can independently change the interaction between them. simply put, the original two directly referenced or dependent objects are taken apart, in the middle of adding a "mediation" object, so that the two objects with the "mediation" object reference or dependency.
For example, the structure below, if each element 22 to create a connection between, the relationship will become complex.
But once the intermediaries are added, they can be decoupled from one another. into a relational structure like the next.
Example
and two with the old Ji are the court minister, but mutual dislike, in the emperor before the and agreeable, secretly each other each other. Of course, sometimes when the emperor promulgated the will to let two ministers work together to complete the task, they will cooperate with each other. and two with the old Ji want to clean up each other, but the two sides of the same office, so when want to clean up each other will be through the emperor to achieve, this time we will be the emperor to understand as intermediaries (feel a bit far-fetched, so it). The following code is used to implement this process.
Official abstract class
/*** Official (minister) Abstract class*/ Public Abstract classOfficial {protectedMonarch Monarch; /*** Every official must have a connection with the monarch .@paramMonarch*/ Publicofficial (Monarch Monarch) { This. Monarch =Monarch; } /*** Add a way to get in touch with an intermediary in an abstract officer class *@paramMonarch*/ Public voidSetmonarch (Monarch Monarch) { This. Monarch =Monarch; }}
Monarch Abstract class
/*** Monarch Abstract class*/ Public Abstract classMonarch {/*** The monarch maintains contact with various officials*/ protectedConcurrenthashmap<string,official> officials =NewConcurrenthashmap<>(); /*** Recruitment officer *@paramname *@paramofficial*/ Public voidaddofficial (String name,official official) { This. Officials.put (name,official); } /*** To dismiss an officer for dismissal *@paramname*/ Public voidremoveofficial (String name) { This. Officials.remove (name); } /*** Dealing with the Suriko of each officer@paramname *@paramMethod*/ Public Abstract voidExecute (String name,string method);}
Lao Ji
/*** Lao Ji*/ Public classLaojiextendsofficial{/*** Every official must have a connection with the monarch .@paramMonarch*/ PublicLaoji (Monarch Monarch) {Super(monarch); } /*** Do your job .*/ Public voidSelf () {System.out.println ("Memorializes Emperor, Chen Lao Ji participated in the preparation of the Siku book has been completed." "); } /*** Join other officials or cooperate with other officials*/ Public voidout () {System.out.println ("Memorializes Emperor, Minister of the old minutes and 21, show female selection work to the present has not been completed, it should be dismissed and prosecuted." "); Super. Monarch.execute ("HeEr", "self"); }}
and two
/*** and two*/ Public classHeErextendsOfficial {/*** Every official must have a connection with the monarch .@paramMonarch*/ PublicHeEr (Monarch Monarch) {Super(monarch); } /*** Do your job .*/ Public voidSelf () {System.out.println ("Memorializes Emperor, minister and two in charge of the show female selection work has been completed." "); } /*** Join other officials or cooperate with other officials*/ Public voidout () {System.out.println ("Memorializes Emperor, minister and two to join the old one, Siku book to the present has not been written to complete, it should be Wenzhan." "); Super. Monarch.execute ("Laoji", "self"); }}
Yellow (Emperor Qianlong)
/*** YELLOW (Emperor)*/ Public classHuangsanextendsMonarch {/*** Dealing with the Suriko of each officer * *@paramname *@paramMethod*/@Override Public voidExecute (string name, String method) {//do your part. if("Self". Equals (method)) { if("Laoji". Equals (name)) {(Laoji)Super. Officials.get (name)). self (); }Else{(HeEr)Super. Officials.get (name)). self (); } //I'm going to join someone else .}Else { if("Laoji". Equals (name)) {(Laoji)Super. Officials.get (name)). Out (); }Else{(Laoji)Super. Officials.get (name)). Out (); } } }}
Test class
Public classClient { Public Static voidMain (string[] args) {//Create a MonarchMonarch Monarch =NewHuangsan (); //creation of two officialsLaoji Laoji =NewLaoji (Monarch); HeEr HeEr=NewHeEr (Monarch); //the monarch and two officials established a relationshipMonarch.addofficial ("Laoji", Laoji); Monarch.addofficial ("HeEr", HeEr); //There is Ben, no Ben back. //laoji.self ();laoji.out (); System.out.println ("---------------memorializes is complete."); //heer.self ();heer.out (); System.out.println ("---------------memorializes is complete."); }}
Run results
Memorializes Emperor, Minister of the old minutes and 21, show female selection work to now not complete, it should be dismissed and prosecuted. Memorializes Emperor, minister and two in charge of the show female selection work has been completed. --------------- memorializes finished memorializes Emperor, minister and two to the old Ji one, Siku book to the present has not been written to complete, it should be wenzhan. Memorializes Emperor, Minister Lao Ji participated in the preparation of the Siku book has been completed. ---------------Memorializes's finished.
The above example is the implementation of the intermediary mode, the emperor became the old and two intermediary. This makes two ministers do not have direct mutual godless, in addition to doing their own job can also urge other colleagues to complete the work.
Structure of the iterator pattern
The constituent roles of the mediator pattern are as follows
Abstract Mediator (mediator) role : The abstract mediator role defines a unified interface for communication between colleague roles. In the above example, the Monarch class represents this role.
specific Mediator (concretemediator) role : The specific mediator role enables collaborative behavior by coordinating the roles of colleagues to guide and reference each colleague role. The emperor represents this role in the above example.
colleague (colleague) role : Each colleague role is aware of the specific mediator role, and when communicating with other colleague roles, be sure to collaborate through the mediator role. In the above example, the old Jamie and two are the representatives of this role.
Summarize
The mediator pattern turns a mesh system structure into a star structure centered on the mediator object, in which a one-to-many relationship between the intermediary object and other objects is used to replace the many-to-many relationship between the original objects.
Advantages
1, the intermediary mode simplifies the interaction between objects, it replaces the original colleague's many-to-many interaction with the intermediary and colleague's one-to-many interaction, the one-to-many relationship is easier to understand, maintains the extension, transforms the originally incomprehensible network structure into the relatively simple star structure.
2. The intermediary mode can decouple the colleagues ' objects. Intermediaries are conducive to the loose coupling between colleagues, we can independently change and reuse each colleague and intermediary, the addition of new intermediaries and new colleagues are more convenient, better in line with the "open and close principle."
3, can reduce the sub-class generation, the intermediary will be distributed across multiple objects in the behavior of the group together, change these behaviors only need to generate new mediator subclass, which makes the individual colleague classes can be reused, do not need to extend the colleague class.
Disadvantages
The inclusion of a large number of interaction details between colleagues in a specific mediator class can lead to a very complex class of intermediaries, making the system difficult to maintain.
Applicable scenarios
1. There are complex reference relationships between objects in the system, the system structure is chaotic and difficult to understand.
2. An object is difficult to reuse because it references many other objects and communicates directly with these objects.
3, want to encapsulate the behavior of multiple classes through an intermediate class, but do not want to generate too many subclasses. It can be implemented by introducing a mediator class, which defines the public behavior of object interaction in the mediator, and adds a new specific mediator class if the behavior needs to be changed.
To learn more about design patterns, see the Java Design Patterns Learning record-gof design pattern Overview.