Intermediary model
Object-oriented design encourages the distribution of behaviors across objects, which can lead to many connections between objects. In the worst case, each object needs to know all other objects.
Although splitting a system into many objects can enhance reusability, the surge in interconnected objects will reduce its reusability. A large number of connection relationships make it impossible for an object to work without the assistance of other objects (the system manifests as an indivisible whole), and it is difficult to make any major changes to the system behavior at this time. Because the behavior is distributed among many objects, the result is that many subclasses have to be defined to customize the behavior of the system. Therefore, we introduced the Mediator object Mediator :
Through the intermediary object, the system of the mesh structure can be transformed into a star structure centered on the intermediary . Each specific object no longer has direct relationship with another object, but is mediated from it through the intermediary object. The introduction of the intermediary object also makes the system structure not undergo a large number of modifications due to the introduction of new objects.
Mediator mode: also known as mediator mode , uses a mediator object to encapsulate the interaction of a series of objects, so that each object does not need to be displayed and referenced to each other, thereby loosening the coupling and changing their interactions independently :
(Image source: Design pattern: The basis of reusable object-oriented software) Tips: Each Colleague only knows the existence of Mediator , and does not need to know whether other Colleagues exist (or how to decouple them). It only needs to send a message to the Mediator, and then forward it to other Colleagues ( the Mediator stores all Colleague relationships, and only Mediator knows how many/which Colleagues are there).
Mode implementation
The United Nations forwards statements from various countries and mediates relations with each country:
States send and receive messages to the UN Security Council, which 'appropriately' forward requests among States for collaborative action:
College
Abstract colleague class, define the public methods of each colleague:
/** * @author jifang * @since 16/8/28 4:22 pm. */public abstract class Country { protected UnitedNations mediator; private String name; public Country(UnitedNations mediator, String name) { this.mediator = mediator; this.name = name; } public String getName() { return name; } protected abstract void declare(String msg); protected abstract void receive(String msg);}--------------------------------------------------------------------------------
ConcreteColleague
Specific colleagues:
•Every colleague class knows its mediator object.
•Each colleague object communicates with its intermediary when it needs to communicate with other colleagues.
class USA extends Country { public USA(UnitedNations mediator, String name) { super(mediator, name); } @Override public void declare(String msg) { mediator.declare(this, msg); } @Override public void receive(String msg) { System.out.println("United States received: [" + msg + "]"); }}class Iraq extends Country { public Iraq(UnitedNations mediator, String name) { super(mediator, name); } @Override public void declare(String msg) { mediator.declare(this, msg); } @Override public void receive(String msg) { System.out.println("Iraq received: [" + msg + "]"); }}class China extends Country { public China(UnitedNations mediator, String name) { super(mediator, name); } @Override public void declare(String msg) { mediator.declare(this, msg); } @Override public void receive(String msg) { System.out.println("China received: [" + msg + "]"); }}--------------------------------------------------------------------------------
Mediator
Abstract Mediator: Define an interface for communicating with colleague objects:
public abstract class UnitedNations { protected List<Country> countries = new LinkedList<>(); public void register(Country country) { countries.add(country); } public void remove(Country country) { countries.remove(country); } protected abstract void declare(Country country, String msg);}--------------------------------------------------------------------------------
ConcreteMediator
Specific intermediary:
• Understand and maintain its individual colleagues;
•Realize collaborative behavior by coordinating each colleague object (receive messages from colleagues and issue orders to specific colleagues).
class UnitedNationsSecurityCouncil extends UnitedNations { /** * The Security Council mediates in the middle* * @param country * @param msg */ @Override protected void declare(Country country, String msg) { for (Country toCountry : countries) { if (!toCountry.equals(country)) { String name = country.getName(); toCountry.receive(name + "Say peacefully: " + msg); } } }} If there is no extension, the Mediator can be combined with the ConcreteMediator into one.
•Client
public class Client { @Test public void client() { UnitedNations mediator = new UnitedNationsSecurityCouncil(); Country usa = new USA(mediator, "USA"); Country China = new China(mediator, "China"); Country iraq = new Iraq(mediator, "Iraq"); mediator.register(usa); mediator.register(china); mediator.register(iraq); usa.declare("I want to attack Iraq, who cares about who I am in a hurry!!!"); System.out.println("----------------"); china.declare("We strongly condemn!!!"); System.out.println("-----------------------"); iraq.declare("Come on, come hurt each other!!!"); }}--------------------------------------------------------------------------------
summary
The emergence of Mediator reduces the coupling between each College, allowing the Colleague and Mediator to be independently changed and reused. Because the objects collaborate, the mediation is taken as an independent concept and encapsulated in an object, the focus of attention is shifted from the behavior of each of the objects itself to the interaction between them, so that the system can be viewed from a more macro perspective.
•applicability
The intermediary model is easy to apply in the system and is easily misused in the system. When a "many-to-many" complex object group appears in the system, do not rush to use the intermediary. It is best to first reflect on whether the system design is reasonable. Since ConcreteMediator controls centralization, the interaction complexity becomes the complexity of the intermediary, making the intermediary more complex than any ConcreteColleague. It is recommended to use the intermediary model in the following situations:
◦ A group of objects communicate in a well-defined but complex way. The resulting interdependencies are confusing and difficult to understand.
◦ An object refers to many other objects and communicates directly with these objects, making it difficult to reuse the object.
◦ Want to customize a behavior distributed among multiple classes, but don't want to generate too many subclasses.
•Related Modes
◦Facade is different from mediators in that it abstracts an object subsystem, thus providing a more convenient interface. Its protocol is unidirectional, that is, Facade objects make requests to this subsystem class, but otherwise cannot. On the contrary, Mediator provides collaboration behaviors that are not supported or cannot be supported by each College object, and the protocol is multi-directional.
◦Colleague can communicate with Mediator using Observer mode.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.