Mediator definition: Use a mediator object to encapsulate a series of interaction behaviors about object.
Why use Mediator mode/mediation mode
There are many interactions between objects. Each object's behavioral operations depend on each other. Modify the behavior of one object, and it will also involve modifying the behavior of many other objects. If you use the Mediator mode, you can make the coupling between each object. Loosely, just care about the relationship with Mediator, turning a many-to-many relationship into a one-to-many relationship can reduce the complexity of the system and improve the modifiable scalability.
How to use the mediation mode
First there is an interface to define the interaction between member objects:
The code copy is as follows:
public interface Mediator { }
The specific implementation of Meiator, which truly implements interactive operations:
The code copy is as follows:
public class ConcreteMediator implements Mediator {
//Suppose there are currently two members.
private ConcreteColleague1 colleague1 = new ConcreteColleague1();
private ConcreteColleague2 colleague2 = new ConcreteColleague2();
...
}
Let’s take a look at another participant: because members are interactive behaviors, they all need to provide some common interfaces. This requirement is the same in modes such as Visitor Observer.
The code copy is as follows:
public class College {
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 Mediator and contact Mediator, not other members.
At this point, the Mediator mode framework has been completed. It can be found that the Mediator mode does not have many regulations, and the general framework is relatively simple, but it is very flexible to use.
Mediator mode is more common in event-driven applications, such as interface design GUI, chat, messaging, etc. In chat applications, there is a MessageMediator, which is specifically responsible for the task adjustment between request/reponse.
MVC is a basic mode of J2EE. View Controller is a Mediator, which is a Mediator between JSP and applications on the server.