Design pattern (behavioral) Mediator mode (mediator pattern)

Source: Internet
Author: User

PS One sentence: Eventually choose Csdn to organize the publication of the knowledge points of these years, the article parallel migration to CSDN. Because CSDN also support markdown grammar, Ah!

"Craftsman Joshui Http://blog.csdn.net/yanbober" read the previous "design pattern (behavioral) Responsibility chain mode (Chain of Responsibility pattern) http://blog.csdn.net/ yanbober/article/details/45531395

Overview

If the connection between objects is presented as a mesh structure, there is a large number of many-to-many connections, in which almost every object needs to interact with other objects, and this interaction behaves as a direct coupling of an object to another object, which results in an over-coupled system. If there are many-to-many relationships between objects in a system, we can separate some of the interactions between objects from each object, and encapsulate them in a mediator object, uniformly coordinated by the mediator, so that many-to-many complex relationships between objects are transformed into relatively simple one-to-many relationships. By introducing intermediaries to simplify the complex interaction between objects, the mediator pattern is a typical application of the "Dimitri Law".

Core

concept: Using a Mediation object (mediator) to encapsulate a series of object interactions, the mediator makes the objects do not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them. The mediator pattern, also known as the mediator pattern, is an object-behavioral pattern.

Intermediary model structure important core module:

Mediator (abstract mediator)

It defines an interface that is used to communicate with each colleague object.

Concretemediator (Specific intermediary)

It is a subclass of an abstract mediator that enables collaborative behavior by coordinating individual colleague objects, which maintains a reference to individual colleague objects.

Colleague (abstract colleague Class)

It defines methods that are common to each colleague class and declares some abstract methods for subclasses to implement, while maintaining a reference to the abstract mediator class whose subclasses can communicate with the mediator through the reference.

Concretecolleague (Specific colleague Class)

It is the subclass of the abstract colleague class; Each colleague object communicates with the intermediary when it needs to communicate with other colleague objects, communicates indirectly with other colleague classes through the mediator, and implements the abstract method declared in the abstract colleague class in the specific colleague class.

Usage Scenarios

There are complex referential relationships between objects in the system, and the system is chaotic and difficult to understand.

An object is difficult to reuse because it references many other objects and communicates directly with those objects.

You want to encapsulate the behavior in multiple classes through an intermediate class, without having 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.

Program Ape Instance

Here is an example of a broker pattern (simulation of a headhunter's dialogue with programmers), strict adherence to the model's core templates, and not much explanation:

 PackageYanbober.github.io;//colleague (Abstract colleague Class)AbstractClass Colleague {protectedString name;protectedMediator mediator; Public colleague(String name, mediator mediator) { This. name = name; This. Mediator = Mediator; }}//mediator (abstract mediator)AbstractClass Mediator { Public Abstract void constact(String message, colleague colleague);}//concretecolleague (Specific colleague Class)Class Concretecolleaguehr extends colleague { Public Concretecolleaguehr(String name, mediator mediator) {Super(name, mediator); } Public void constact(String message) {mediator.constact (message, This); } Public void GetMessage(String msg) {System.out.println ("hr#"+name+"#:"+MSG); }}class Concretecolleagueandroiddeveloper extends Colleague { Public Concretecolleagueandroiddeveloper(String name, mediator mediator) {Super(name, mediator); } Public void constact(String message) {mediator.constact (message, This); } Public void GetMessage(String msg) {System.out.println ("Android developer#"+name+"#:"+MSG); }}//concretemediator (Specific intermediary)Class Concretemediator extends Mediator {PrivateConcretecolleaguehr hr;PrivateConcretecolleagueandroiddeveloper AD; PublicConcretecolleaguehrGetconcretecolleaguehr() {returnhr } PublicConcretecolleagueandroiddeveloperGetconcretecolleagueandroiddeveloper() {returnAd } Public void Setconcretecolleaguehr(Concretecolleaguehr hr) { This. hr = HR; } Public void Setconcretecolleagueandroiddeveloper(Concretecolleagueandroiddeveloper AD) { This. AD = AD; }@Override     Public void constact(String message, colleague colleague) {if(colleague = = hr)        {ad.getmessage (message); }Else{hr.getmessage (message); }    }}//Client Public  class Main {     Public Static void Main(string[] args) {Concretemediator Mediator =NewConcretemediator (); Concretecolleaguehr hr =NewConcretecolleaguehr ("Google recruiting specialist", mediator); Concretecolleagueandroiddeveloper AD =NewConcretecolleagueandroiddeveloper ("Dick Wire Developer", mediator);        Mediator.setconcretecolleaguehr (HR);        Mediator.setconcretecolleagueandroiddeveloper (AD); Hr.constact ("Hi, are you interested in our company?" "); Ad.constact ("is Google developing Android?" "); Hr.constact ("yes!"); Ad.constact ("I am willing!" "); }}
sum up a

Broker Mode Advantages:

The interaction between objects is simplified, and it replaces many-to-many interactions between the original colleagues with a one-to-many interaction between the mediator and the colleague, and a one-to-many relationship is easier to understand, maintain, and extend, transforming the previously incomprehensible mesh structure into a relatively simple star structure.

You can decouple each colleague object.

Can reduce the generation of subclasses, the mediator will be distributed across multiple objects in the behavior of the group together, change these behaviors only need to generate a new mediator subclass, which makes the individual colleague classes can be reused, do not need to extend the colleague class.

Broker Mode Disadvantage:

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.

"Craftsman Joshui Http://blog.csdn.net/yanbober" Continue reading "to be continued ... 》

Design pattern (behavioral) Mediator mode (mediator pattern)

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.