Intermediary

motivation

  • In the process of software construction, there are often situations where multiple objects are related to each other and interact with each other. A complex reference relationship is often maintained between objects. If some demand changes are encountered, this direct reference relationship will face constant changes. .
  • In this case, we can use an "intermediary object" to manage the relationship between objects, avoid the tightly coupled reference relationship between interacting objects, and thus better resist changes.

Mode definition

  • Use an intermediary object to encapsulate (encapsulate changes) a series of object interactions. The intermediary makes each object do not need to explicitly refer to each other (compile-time dependency -> runtime dependency), thereby loosening the coupling (managing changes), and can independently change the interaction between them.

Summary of main points

  • To decouple the complex association relationship between multiple objects, the Mediator mode centrally manages the control logic between multiple objects, changing "multiple objects are associated with each other" to "multiple objects are associated with an intermediary", which simplifies the system Maintenance and resistance to possible changes.
  • With the complexity of the control logic, the realization of the specific objects of the Mediator may be quite complicated. At this time, the Mediator object can be decomposed.
  • Facade mode is a decoupling system (one-way) object association relationship; Mediator mode is a decoupling system between each object (two-way) association relationship.

Guess you like

Origin blog.csdn.net/oTianLe1234/article/details/113921744