The mediator pattern for Java design patterns

Source: Internet
Author: User

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.

Mediator Interface Code:

1  /** 2 2  * Mediator interface 33  */4publicInterface Mediator {5 5     void Change (String Message,zhiyuan zhiyuan,string nname); 6 6}

Staff abstract Category:

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 classJingliImplementsMediator {5 @Override6      Public voidChange (String Message,zhiyuan zhiyuan,string nname) {7SYSTEM.OUT.PRINTLN ("Manager receives" + Nname + "needs:" +message);8System.out.println ("Manager will send" + nname + "requirements to 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}

Results:

Staff A initiates demand: This information requires B staff to operate
Manager receives staff A's needs: This information requires B staff to operate
The manager sends the staff A's needs to the target staff
Staff B receives a request from staff A: this information needs to be operated by Staff B
Staff C Initiates demand: This information requires B staff to sign
The manager received the request from Clerk C: This information needs to be signed by Clerk B.
The manager sends the staff C's needs to the target staff
Staff B receives a request from Clerk C: This 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.

Reprint: http://www.cnblogs.com/V1haoge/p/65186783.html

The mediator pattern for Java design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.