Preliminary Research on the Design Mode-intermediary Mode

Source: Internet
Author: User

The MEDIATOR mode, also known as the MEDIATOR mode, encapsulates a series of object interactions by using an intermediary object. It is an object behavior mode. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently. In real life, the intermediary model is everywhere. Up to the solemn court, down to the marriage agency or dating platform that brings the gospel to programmers, and the hateful real estate agency, are the embodiment of the intermediary model. For example, if you participate in a dating activity, more than 20 men and women are eager to find their other half. If you don't fall in love at first sight, you will need to know more than 20 girls. If you don't rule out boys, you will be more than 40 people's preferences. It is a disaster to know if your personality fits your needs! If there is an official matchmaker who matches with each other and matches more than 20 pairs of men and women based on their preferences and personalities, this reduces the coupling relationship between the two, saving time and energy for those who are close to each other is a wonderful use of the intermediary model.

I. Application scenarios

1. A group of objects communicate in a well-defined but complex way, such as a network-like structure. This creates a chaotic and hard-to-understand dependency structure. You can use the intermediary mode to transform complex dependencies for better flexibility.

2. An object references many other objects and communicates directly with these objects, making it difficult to reuse the object. The intermediary mode converts the object dependency to the dependency between the intermediary and the object, enabling flexible reuse of the object.

3. I want to customize a behavior distributed across multiple classes without generating too many child classes.

Ii. UML diagram

Iii. Java implementation

 

Package study. patterns. mediator; import java. util. arrayList; import java. util. list;/*** the intermediary mode converts a mesh system structure into a star structure centered on the intermediary object. * In this star structure, use the one-to-multiple relationship between the intermediary object and other objects to replace the many-to-many relationship between the original objects. * The intermediary mode is widely used in event-driven software, especially applications based on the GUI (Graphical User Interface, * Graphical User Interface). In addition, * The intermediary mode can be well applied in systems with complex associations between classes. * @ Author qbg */public class MediatorPattern {public static void main (String [] args) {List
 
  
Ps = new ArrayList
  
   
(); Person b0 = new Boy (); ps. add (b0); Person b1 = new Boy (); ps. add (b1); Person g0 = new Girl (); ps. add (g0); Person g1 = new Girl (); ps. add (g1); Mediator mediator = new ConcreteMediator (ps); b0.setInterest (entrepreneurship); b0.setMediator (mediator); b1.setInterest (earning money); b1.setMediator (mediator); g0.setInterest (shopping ); g0.setMediator (mediator); g1.setInterest (home holding); g1.setMediator (mediator); b0.interest (home holding); System. out. println (========== ==============================); G0.interest (earning money );}} /*** intermediary abstract interface */interface Mediator {public void interestChanged (Person p);}/*** a specific intermediary acts as a matchmaker. */Class ConcreteMediator implements Mediator {private List
   
    
Miai; // public ConcreteMediator (List
    
     
Ps) {this. miai = ps;}/*** response when the interest of the person is changed and the candidate is re-matched. * encapsulate interaction between colleague objects. * // @ Overridepublic void interestChanged (Person p) {boolean succeed = false; Person candidate = null; for (Person person: miai) {// The match is successful if the target user is not the same as the target user! If (person! = P & p. interest. equals (person. interest) {succeed = true; candidate = person; break ;}} p. update (succeed); if (candidate! = Null) {candidate. update (succeed) ;}}/ *** Person, abstract colleague class */abstract class Person {protected Mediator mediator; // The intermediary references protected String interest; // interest public void setMediator (Mediator m) {this. mediator = m;} public void setInterest (String in) {this. interest = in;} public void interest (String interest) {this. interest = interest; mediator. interestChanged (this);} public abstract void update (boolean succeed);}/*** boys, specific Colleague class */class Boy extends Person {@ Overridepublic void update (boolean succeed) {if (succeed) {System. out. println (a successful blind date, a big drink !!!);} Else {System. out. println (the blind date fails. Fight again tomorrow !!!);}}} /*** Girl, specific colleague class */class Girl extends Person {@ Overridepublic void update (boolean succeed) {if (succeed) {System. out. println (find Prince Charming and go to KTV !);} Else {System. out. println (not found in the Black Donkey, all of them are donkeys, whining ....);}}}
    
   
  
 
Running result:

 

 

 

Iv. Advantages and Disadvantages

Advantages:

1. Subclass generation is reduced. Mediator gathers the behavior originally distributed among multiple objects. To change these actions, you only need to generate a subclass of Mediator, so that the colleags can be reused.

2. decouple colleags. Mediator is conducive to loose coupling between different colleags. You can independently change and reuse different colleags and Mediator classes.

3. simplified the object protocol. The one-to-many interaction between Mediator and Colleague replaces the multi-to-many interaction, making the object relationship easier to understand, maintain, and expand.

4. Abstract How objects collaborate. The intermediary mode transfers users' attention from the actions of the objects themselves to the interaction between them.

5. centralized control. The intermediary mode changes the complexity of interaction into the complexity of the intermediary.

Disadvantages:

1. Because the intermediary mode focuses the control on the intermediary, it may be too complicated and difficult to maintain the intermediary.

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.