Introduction to the mediation mode of Java design pattern (mediator mode) _java

Source: Internet
Author: User

Mediator definition: Encapsulates a series of object interaction behaviors with a mediation object.

Why use Mediator Mode/mediation mode

The interaction between the objects is very many, each object's behavior operations are dependent on each other, modify the behavior of an object, but also involves the modification of many other objects of the behavior, if the use of mediator mode, can make the coupling between the various objects loosely, just care and mediator relationship, The relationship between Many-to-many and Many-to-many becomes a one-to-many relationship, which can reduce the complexity of the system and improve the scalability of modification.

How to use mediation mode

First, there is an interface to define how the member objects interact with each other:

Copy Code code as follows:

Public interface Mediator {}

Meiator concrete implementation, the actual implementation of the content of the interactive operation:
Copy Code code as follows:

Public class Concretemediator implements mediator {
Suppose there are currently two members.
Private ConcreteColleague1 colleague1 = new ConcreteColleague1 ();
Private ConcreteColleague2 colleague2 = new ConcreteColleague2 ();
...
}

Then look at another participant: Members, because they are interactive, require that both sides provide some common interface, which is the same in visitor observer and other modes.
Copy Code code as follows:

public class Colleague {
Private mediator mediator;
Public Mediator Getmediator () {
return mediator;
}
public void Setmediator (mediator mediator) {
This.mediator = Mediator;
}
}
public class ConcreteColleague1 {}
public class ConcreteColleague2 {}

Each member must know the mediator and contact the mediator, not with the other members.

At this point, the Mediator model framework is completed, you can find that the mediator model rules are not many, the general framework is relatively simple, but the actual use of very flexible.

Mediator mode in the event-driven class applications, such as interface design GUI, chat, messaging, etc., in the chat application, need to have a messagemediator, specifically responsible for the request/reponse between the task of adjustment.

MVC is a basic pattern of Java EE, View controller is a kind of mediator, it is the mediator between the JSP and the application on the server.

Related Article

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.