The intermediary mode of Java design pattern

Source: Internet
Author: User

First, a primer

Intermediary in the real life is not unfamiliar, the streets of the housing intermediary, mixed with the overseas intermediary .... They exist because they can bring some convenience to our lives: rent, buy a house do not need to wander around the community, and study abroad do not have to be overwhelmed.

The intermediary model also plays a similar role in program design.

II. Definition and structure

The definition of GOF to the mediator pattern is to encapsulate a series of object interactions with a mediation object. Intermediaries do not need to refer explicitly to each other, so that they are loosely coupled and can independently alter their interactions. In simple terms, the original two direct references or dependent objects are disassembled, and a "mediation" object is added to the middle to make the two ends of the object and the "mediation" object referenced or dependent.

Of course, not all objects need to join the "mediation" object. If the relationship between the objects is clear, the intermediary object to join is "superfluous".

Look at the components of the mediator model.

1 abstract Intermediary (mediator) role: The abstract mediator role defines a unified interface for communication between peer roles.

2 The specific mediator (concrete mediator) role: The specific mediator role implements collaborative behavior by coordinating the roles of each colleague. To do this it needs to know and refer to the roles of each colleague.

3 colleague (colleague) role: each colleague role knows the specific mediator role, and when communicating with other colleagues, be sure to collaborate through an intermediary role.

Class diagram from the Book of Design Patterns:

Since the behavior of the mediator is closely related to the data to be used, it is not realistic for the abstract broker role to provide an interface that is convenient for many objects to use. So the role of the abstract mediator is often non-existent, or just a sign interface. If it is fortunate to be able to extract the abstract intermediary role that really carries the action, I think the choice of the coworker role for the specific mediator role is also an application of the strategy.

"Just right, too." It's the best that suits your system.

Iii. further discussion

Do you remember which of the three tiers the broad MVC application is divided? Model layer, Presentation layer (View) and Control layer (control\mediator). The control layer is the intermediary between the presentation layer and the model layer. Generally speaking, MVC is also considered an intermediary model in the framework design of an application.

Because the mediator pattern is loosely defined, it is similar in structure to the Observer pattern and command pattern, and the application purpose is somewhat like the "façade mode" of the structural pattern.

Structurally, the mediator pattern and the Observer pattern, the command pattern, all add intermediate objects--but the mediator removes the latter both in the direction of the behavior. Therefore, the intermediary's application can be modeled after the two examples to write. But the observer pattern, the Observer in the command mode, the commands are known to the customer, the specific observer, the application of the command are specified by the customer, and most of the intermediary role is transparent to the client program. Of course, the reason for this difference is that they want to achieve different purposes.

From the point of view, the intermediary model has nothing to do with the observer pattern and the command mode, but it is somewhat similar to the façade pattern mentioned earlier.

But the façade mode is between the client program and the subsystem, and the intermediary mode is between subsystem and subsystem. This also doomed them to have the very big difference: the façade pattern is extracts the original complex logic to a unified interface, simplifies the customer to use logically. It is perceived by the client, and the original complex logic is hidden. And the addition of the intermediary model does not change the customer's original use habits, it is hidden behind the original logic, making the code logic more clearly available.

The characteristics of the intermediary model have been written out in succession in the previous land. Here's another summary. The biggest benefit of using the mediator pattern is decoupling the coworker role. This brings a series of system structure improvement: improve the original system readability, simplify the communication protocol of the original system-more than the original multiple pairs of more than a one-to-many, improve the reusability of the code ...

But the mediator's role focuses too much responsibility, and all of the coworker's objects are controlled by it. This reminds me of the simple factory pattern, but because of the particularity of the mediator pattern--closely related to the business logic, it is not possible to adopt a solution similar to the factory method pattern. It is therefore recommended that you control the size of the mediator role when using mediator mode.

discussed so much about the characteristics of the intermediary model. It can be summed up in the use of intermediary mode: A group of objects in a well-defined but complex way to communicate, resulting in a messy dependency, but also caused the object difficult to reuse.

Iv. Summary

The intermediary model is easy to use in the system and is easily misused in the system. When the system appears "Many-to-many" Interactive complex object group, do not rush to use the intermediary model, but first to reflect on the design of your system is not reasonable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.