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.