Dahua Design Patterns Twenty-three Mediator Patterns

The mediator model is also called the mediator model. In fact, it means a middleman or mediator.


Recently, there have been many explosions of launch tubes in Iraq. The war has brought irreparable pain to mankind. The world needs peace. If it weren't for the Iraq war, there probably wouldn't have been so much. Another example is the Palestine-Israel issue, the Iranian nuclear issue, the North Korean nuclear issue and the political and diplomatic issues between countries, which constitute an extremely complex international form.

Due to the different interests represented by countries, conflicts are inevitable. However, if such an organization is composed of representatives of various countries, it is used to maintain international peace and security and resolve international economic, social, cultural and humanitarian issues. Problems, such as the United Nations Organization, it is a mediator, intermediary role.

The relationship between countries is complex, strategic allies, strategic partners, strategic opponents, stakeholders, etc., all countries need to invest a lot of manpower and material resources in politics, economy and diplomacy to improve these relations, but no matter how hard they work, the relationship between countries The relationship between them will change with time and social development. Before the Second World War, because there was no such a democratic and neutral coordination organization, the fascist alliance emerged, which caused the greatest disaster in human history-World War II. Since the establishment of the United Nations in 1945, there has been no worldwide war on earth. It can be said that the contribution of the United Nations to world peace is immeasurable.


The relationship between countries is similar to the relationship between different objects, which requires that objects need to know all other objects.

While dividing a system into many objects generally increases its reusability, the proliferation of interconnections between objects reduces its reusability.

Because a large number of connections make it impossible for one object to work without the support of other objects, the system behaves as an indivisible whole, so it is very difficult to make any major changes to the behavior of the system.


To solve such a problem, the "Law of Demeter" can be applied, if two classes do not have to communicate directly with each other, then the two classes should not interact directly. If one of the classes needs to call a method of the other class, the call can be forwarded through a third party. Here, countries can go through a star-shaped structure centered on the mediator of the 'United Nations'. Each specific object no longer interacts with two objects through a direct connection, but interacts with the 'mediator' object. Another object interacts. The design of the mediator object makes the structure of the system not cause a lot of modification work due to the introduction of new objects.



2, the mediator model

The Mediator pattern uses a mediator object to encapsulate a series of object interactions. Mediators allow objects to be loosely coupled without having to explicitly call each other, and can independently alter their interactions.


Colleague is called an abstract colleague class, and ConcreteColleague is a concrete colleague class. Each concrete colleague only knows its own behavior and does not know the situation of other colleague classes, but they all know the mediator object. Mediator is an abstract mediator, which defines the simultaneous The interface between the object and the mediator object, ConcreteMediator is the concrete mediator object, which implements the methods of the abstract class. It needs to know all the concrete colleague classes, receive messages from the concrete colleague, and issue commands to the concrete colleague.





Due to the Mediator, ConcreteCollegue1 and ConcreteColleague2 actually go through the mediator when sending and receiving messages, which reduces the coupling between them.

Demand: The dialogue between the United States and Iraq is done through the intermediary of the UN Security Council.

The United States and Iraq are both countries, and one country abstract class and two concrete interstate classes are sufficient. But is 'United Nations' a Mediator or a ConcreteMediator?

This depends on whether it is possible to expand the intermediary object in the future. For example, is there any other organization in the United Nations besides the Security Council?

There are also United Nations agencies such as the Inter-State Labour Organization, UNESCO, World Health Organization, World Trade Organization, etc., so jMediator should be the 'United Nations agency', and the 'Security Council' is a specific intermediary.

If no extension exists, then Mediator can be combined with ConcreteMediator.


3. The Security Council acts as an intermediary






Although this design can reduce the coupling between ConcreteColleague classes, it makes ConcreteMediator too much responsibility, and if it goes wrong, the whole system will have problems.

4. Advantages and disadvantages of the intermediary model

The mediator pattern is easy to apply in the system and easy to misuse in the system. When there is a 'many-to-many' complex object group in the system, don't rush to use the mediator pattern, but first reflect on whether your system is reasonable in design.


The advantage of the mediator pattern is that the emergence of Mediator reduces the coupling of each Colleagued, so that each Colleague class and Mediator can be changed and reused independently.

For example, changes in each country will not affect other countries, but at least changes with the Security Council.

Secondly, because how the objects cooperate is abstracted, the mediation is regarded as an independent concept and encapsulated in an object, so that the objects of concern are transferred from the behavior of the objects themselves to the interaction between them, that is, the station Look at the system from a larger perspective.

For example, the Palestinian-Israeli conflict can only be regarded as a contradiction between countries, so their views may be relatively narrow, but from the perspective of the UN Security Council, this issue can be viewed from a global and more objective perspective. Contribute to mediation and peacekeeping.



Disadvantages of the mediator pattern:

The concrete mediator class ConcreteMediator may become very complicated as there are more and more ConcreteColleagues, and it is not easy to maintain.

Since ConcreteMediator controls centralization, it turns interaction complexity into mediator complexity, which makes mediators more complex than any ConcreteColleague.

In fact, the work of the Secretary-General of the UN Security Council should be very busy. Who calls him the 'biggest official in the world'? Because of this, the advantage of the mediator model comes from centralized control, and its disadvantage is also it.


The aspx of Form or Web site program in Windows applications written with .NET is a typical intermediary. For example, a calculator program, which has menu controls,

Text controls, multiple button controls, and a Form form, who is responsible for the communication between each control? Are they aware of each other's existence?


The code for each control is encapsulated, so their instances are unaware of the existence of other control objects. For example, if you want to display the number in the text box by clicking the number button, according to the previous idea, you should write the code to assign value to the Text property of the TextBox class instance in the Button class, resulting in the coupling of the two classes, which is obviously very unreasonable. But the actual situation is that they all have an event mechanism, and the execution of the event is completed in the code of the Form form, that is to say, the interaction of all the controls is mediated by the Form form, and each object is operated. This is typical mediator pattern application.


The Mediator pattern is generally used where a set of objects communicate in a well-defined but complex way, and where you want to customize the behavior of one distributed among multiple classes, but don't want to generate too many subclasses.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325459847&siteId=291194637