Design mode Mediator (000holder)

zhaozj2021-02-16  102

Mediator Definition: Use a secondary object to encapsulate a series of object interactions.

Why use Mediator? There are many interactions between individual objects; the behavior operation of each item 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 The coupling between the object is loose, just care about the relationship between the Mediator, makes multiple relationships into a pair of relationships, can reduce the complexity of the system and improve the modification of the scalability.

how to use?

First, there is a interface that defines 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, it requires both parties to provide some common interfaces, which are 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-driven category application, such as interface design gui .; chat, message delivery, etc. In chat applications, you need to have a MessageMediator, which is responsible for the adjustment between the task between the request / Reponse.

The MVC is a basic mode of J2EE. View Controller is a Mediator, which is a Mediator between the JSP and the server.

转载请注明原文地址:https://www.9cbs.com/read-11466.html

New Post(0)