The 19th chapter of the design pattern-the mediator pattern (Java implementation)

Source: Internet
Author: User

The 19th chapter of the design pattern-the mediator pattern (Java implementation)

  "The Test girl paper for you, your code problem." "The artist paper lets you see how the interface is." "As programmers always have to deal with a variety of people, but if divided into front-end, back-end engineers, then the relationship will be complicated, this time if there are intermediaries to relay, similar to the Star network topology of the switch, then how good." (PS: Solitary born Ah, and test sister paper, art sister paper and so on to discuss how to improve feelings, so good opportunities are not cherished. Editor's note: I'm going to be the intermediary, okay? The Mediator! Everyone: Good job!) "fish brother, what do you call me?" "Really said Cao Cao to, just said you again, OK, you say it."

Self-introduction of the intermediary model

I am the legendary intermediary, aka "mediator", defined as follows: Define an object, which encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to all other explicitly, and it lets you vary their in Teraction independently. A mediation object encapsulates a series of object interactions so that the objects do not need to be shown to interact with each other, so that they are loosely coupled and can independently change the interaction between them. The general class diagram is as follows:

Self-analysis of the intermediary model

  All speaking of this, nature should also be a simple analysis of their own:

Advantage:

    • The generation of subclasses has been reduced. So that each colleague class can be reused.
    • Decouple the individual colleague. In this way, various colleague and mediator classes can be changed and reused independently.
    • simplifies the object protocol. Makes the mediator and each colleague one-to-many interaction, simplifies the relationship, facilitates the understanding maintenance and the extension.
    • The abstraction of how objects work together helps to understand how individual objects interact in a system.

  Disadvantage:

    • Centralize control, so intermediaries are more complex, and then the intermediaries become difficult to maintain.

The realization of the intermediary mode

  Realize, since you want to see, then I will take a team of the individual parts of a demo it. First I have an abstract colleague class that I want to talk to you about:

1  Public Abstract class abstractcolleague{2     protected abstractmediator Mediator; 3      Public Abstractcolleague (abstractmediator mediator) {4         this. Mediator = Mediator; 5     }6 }

  Next is the concrete implementation of the programmer class, of which there are three methods, two are their own implementation of the method, one is the task of the art of the paper, through the intermediary to the artist sister paper:

1  Public classProgrammerextendsabstractcolleague{2      PublicProgrammer (abstractmediator mediator) {3         Super(mediator);4     }5     //the task of the Art girl paper, through the intermediary to the artist6      Public voidNotifyifyuier () {7System.out.println ("Tell the artist paper to help beautify the interface");8         Super. Mediator.beautifyui ();9     }Ten  One      Public voidcode () { ASYSTEM.OUT.PRINTLN ("Write program, develop"); -     } -  the      Public voidview () { -System.out.println ("See How the Interface"); -     } -}
View Code

  Then is the art of the specific implementation of the paper, one is their own work, a programmer's business, through the intermediary to the programmer:

1  Public classUierextendsabstractcolleague{2      PublicUier (abstractmediator mediator) {3         Super(mediator);4     }5 6     //Programmer's task, through the intermediary to the programmer7      Public voidNotifyprogrammer () {8SYSTEM.OUT.PRINTLN ("Let the programmer look at the interface");9         Super. Mediator.view ();Ten     } One  A      Public voidBeautifyui () { -System.out.println ("Beautify The Interface"); -     } the}
View Code

The next is the play, the finale, that is, the abstraction of the intermediary abstract class implementation:  

1  Public Abstract classabstractmediator{2     //Programmer3     protectedProgrammer Programmer;4     //UI Sister paper5     protectedUier UI;6     //constructor Function7     //set, get method, omitted here8 9     //fish brother is used to deal with relationships between multiple objectsTen      Public Abstract voidBeautifyui (); One      Public Abstract voidview (); A  -}

  Finally, the specific implementation of the Mediation class:

1  Public classMediatorextendsabstractmediator{2     //Fish Brother's coordination strategy3      Public Abstract voidBeautifyui () {4         Super. Ui.beautifyui ();5     }6      Public Abstract voidview () {7         Super. Programer.view ();8     }9}

  In this way, an intermediary pattern is achieved. If there are other related colleagues, such as the test sister paper, marketing, project managers and so on can be added, by the intermediary to associate.

The application scenario of the mediator pattern

Let me introduce you to the circumstances under which I can be used:

    • A set of objects communicates in a well-defined but complex way.
    • An object that references many other objects and communicates directly with those objects makes it difficult to reuse the object.
    • You want to customize a behavior that is distributed across multiple classes without having to generate too many subclasses.

  When you encounter the above or similar situation in the project, you can use me to communicate between the objects, which will simplify the complex problem ~ Although the object is too large, it will lead to the intermediary is too big. Well, brother Fish, I'm done. (PS: Say this to everyone.) Oh, everybody, I'm done. (the author left the 、、、 with a full black line)

PS: This blog welcome forwarding, but please specify the blog address and author ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.net

<. ))) ≦

The 19th chapter of the design pattern-the mediator pattern (Java implementation)

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.