Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/6518603.html
Mediator mode.
Let's imagine a scenario in which a system internally calls each other through a number of classes to accomplish a series of functions, each of which has at least one invocation and is called, and many more, in which case, once a class has a problem, the modification will undoubtedly affect all classes that call it. Even the class that it calls, it is visible in this case, the coupling between classes and classes is extremely high (embodied as too many complex direct references).
This is the home of the mediator mode, the mediator is like a third-party intermediary, the reference between all classes and classes directed to the Mediator class, all classes of the request, uniformly sent to the mediator, from the mediator to the target class, so that the original complex network of the class relationship, a simple star class relationship, the Mediator class is located in the core, All other classes are located on the periphery, pointing to the mediator. In this case, the direct call coupling between classes and classes is lifted (initiated by a unified third party), a problem occurs in a class, changes occur, and only the mediator is affected, without directly affecting the classes that the intro initiates the call.
Here's an example of life: A company department, a manager who acts as a mediator, and its employees act as interacting classes, which is a very vivid example. If the interaction between all employees is carried out directly between employees, and once an employee is absent, then the things that must be done by the employee cannot be interacted with, or an employee is replaced, the employee is not familiar with the interaction, the role of the manager is coming, the employee who initiated the demand tells the manager, The manager then looked for other employees to manipulate the demand, the obvious mediator pattern.
Here's a look at the sample code:
Mediator Interface: Mediator
1 /* * 2 * Mediator Interface 3 */ 4 Public Interface Mediator {5 void Change (String Message,zhiyuan zhiyuan,string nname); 6 }
Staff abstract Category: Zhiyuan
1 /**2 * Staff Interface3 */4 Public Abstract classZhiyuan {5 String name;6 PrivateMediator Mediator;7 PublicZhiyuan (Mediator mediator,string name) {8 This. Mediator =Mediator;9 This. Name =name;Ten } One //method invoked by the mediator A Public voidcalled (String message,string nname) { -SYSTEM.OUT.PRINTLN (name + "received from" + Nname + "needs:" +message); - } the //Call a mediator - Public voidCall (String Message,zhiyuan zhiyuan,string nname) { -System.out.println (Nname + "initiating demand:" +message); - Mediator.change (message,zhiyuan,nname); + } -}
Specific mediators: Jingli
1 /**2 * Mediator: Manager3 */4 Public classJingli implements Mediator {5 @Override6 Public voidChange (String Message,zhiyuan Zhiyuan, String nname) {7System. out. println ("Manager received"+ Nname +"'s needs:"+message);8System. out. println ("Manager Will"+ Nname +"needs to be sent to the target staff");9 Zhiyuan. Called (message,nname); Ten } One}
Specific staff: Zhiyuana, ZHIYUANB, Zhiyuanc
1 /**2 * Staff a3 */4 Public classZhiyuanaextendsZhiyuan {5 PublicZhiyuana (Mediator Mediator, String name) {6 Super(mediator, name);7 }8 }9 Ten /** One * Clerk B A */ - Public classZhiyuanbextendsZhiyuan { - Publiczhiyuanb (Mediator Mediator, String name) { the Super(mediator, name); - } - } - + /** - * Clerk C + */ A Public classZhiyuancextendsZhiyuan { at PublicZhiyuanc (Mediator Mediator, String name) { - Super(mediator, name); - } -}
Test class: Clienter
1 Public classClienter {2 Public Static voidMain (string[] args) {3 //assigning staff and managers4Mediator Jingli =NewJingli ();5Zhiyuan Zhiyuana =NewZhiyuana (Jingli, "clerk a");6Zhiyuan ZHIYUANB =NewZHIYUANB (Jingli, "Clerk B");7Zhiyuan Zhiyuanc =NewZhiyuanc (Jingli, "Clerk C");8 //the needs of staff a9String Messagea = "This data requires B clerk action";Ten Zhiyuana.call (messagea,zhiyuanb,zhiyuana.name); One //request from Clerk C AString Messagec = "This material requires B staff to sign"; - Zhiyuanc.call (MESSAGEC, zhiyuanb,zhiyuanc.name); - } the}
Execution Result:
Employee A initiates demand: This information requires the B Staff Operations Manager to receive staff A's needs: This information requires the B Staff operations Manager to send staff A's needs to the target staff B to receive the requirements from staff A: This information requires B staff operator C to initiate the demand: This information requires the B Staff signature Manager to receive the staff C requirements: This information needs B The officer's signature manager sends the staff C's needs to the target staff B and receives the request from staff C: the information needs to be signed by Clerk B.
As listed above, staff A and employee C are required to request staff B, but if they do not know staff B, then the job needs to be submitted to the manager, the manager will send the job requirements to staff B.
The use of the mediator mode seems to be more than the original structure consumption time, but it will be the initiator of the demand and the performer of the strong coupling between the reduction, greatly optimizing the system internal maintenance work.
The mediator pattern decreases the coupling within the system, and the appearance pattern decreases the coupling between the systems.
The mediator mode is more detailed, aiming at the dissolution of strong coupling between class and class in the system, and the appearance pattern is more integrated, aiming at the decoupling of the whole system from the outside, both of them have the function of shielding complexity.
The "mediator mode" in Java design mode and its application scenario