Mediator Mediator Mode Definition: Use a mediation object to encapsulate a series of object interactions.
Why use Mediator? There are many interactions between individual objects; each object's behavior operation relies on each other to modify the behavior of an object, and it will involve modifying many other objects. If you use Mediator mode, you can make each object. The coupling between the objects is loose, just care about the relationship between the Mediator, so that many-to-many relationships become a pair of relationships, which can reduce the complexity of the system and improve the scalability.
how to use?
First there is an interface to define the interaction between member objects:
Public interface mediator {}
Meiator implementation, truly implementing interactive operation:
public class ConcreteMediator implements Mediator {// assumed that the current has two members private ConcreteColleague1 colleague1 = new ConcreteColleague1 ();. private ConcreteColleague2 colleague2 = new ConcreteColleague2 (); ...}
Take a look at another participant: member, because it is interactive behavior, both parties need to provide some common interfaces, this requirement is the same in Visitor Observer and other modes.
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 {}
Every member must know Mediator and contact Mediator instead of contact with other members.
At this point, the Mediator mode framework can be found that the Mediator mode is not a lot, and the general framework is relatively simple, but it is very flexible in actual use.
Mediator mode is more in the event drive class, such as interface design gui .; chat, message delivery, etc. In chat applications, you need to have a MessageMediator, which is responsible for the regulation of tasks between request / reponse.
MVC is a basic mode of J2EE, and View Controller is a Mediator, which is a Mediator, a JSP and a server.