Observer model, intermediary model

Observer mode

Definition: Refers to the one-to-many dependency relationship between multiple objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.

Pros and cons

advantage:

  1. Reduced the coupling relationship between the target and the observer, which is an abstract coupling relationship;
  2. A trigger mechanism is established between the target and the observer

Disadvantages:

  1. The dependency between the target and the observer is not completely lifted, and there may be circular references;
  2. When there are a lot of observer objects, the announcement of the notification will take a lot of time, which affects the efficiency of the program.

Application scenarios

  1. There is a one-to-many relationship between objects, and a change in the state of one object will affect other objects;
  2. When an abstract pattern has two aspects, one of which depends on the other, the two can be encapsulated in independent objects so that they can be changed and reused independently.

main character

  1. Abstract subject (Subject) role: also called abstract target class, it provides an aggregate class for saving observer objects, methods for adding and deleting observer objects, and abstract methods for notifying all observers.
  2. The role of the concrete subject class (ConcreteSubj): also called the concrete target class. It implements the notification method in the abstract target. When the internal state of the specific subject changes, it notifies all registered observer objects.
  3. Abstract Observer (Observer) role: It is an abstract class or interface, which contains an abstract method to update itself, which is called when receiving a notification of a change in a specific subject.
  4. Concrete Observer (ConcreteObserver) role: to implement the abstract method defined in the abstract observer, in order to update its own state when notified of changes in the target.

Structure chart

Insert picture description here

Instance

Use the observer model to design a program to analyze the impact of the rise and fall of the "Renminbi exchange rate" on importing companies and export companies.
Analysis: When the "Renminbi exchange rate" appreciates, the cost of export products of the importing company will decrease and the profit margin will increase, and the export company's export product income Decrease and lower profit margins; when the "RMB exchange rate" depreciates, the cost of imported products of importing companies will increase and profit margins will decrease, and the income of export companies will increase and profit margins will increase.
The structure diagram is as follows:
Insert picture description here

Mediator mode

Definition: Define an intermediary object to encapsulate the interaction between a series of objects , loosen the coupling between the original objects, and independently change the interaction between them. The intermediary model is also called the mediation model, which is a typical application of Dimit's law.

Pros and cons

advantage

  1. Reduce the coupling between objects, making the objects easy to be reused independently;
  2. Convert one-to-many associations between objects into one-to-one associations , improve the flexibility of the system, and make the system easy to maintain and expand;

Disadvantages: When there are too many simultaneous classes, the intermediary's responsibilities will be very large, it will become complex and large, so that the system is difficult to maintain .

Application scenarios

  1. It can be used when there is a complex network structure relationship between the objects, which causes the dependency relationship to be chaotic and difficult to reuse;
  2. It is available when you want to create an object that runs between multiple classes, but do not want to generate new subclasses.

main character

  1. Abstract mediator (Mediator) role: It is the interface of concrete mediators, and it provides abstract methods for colleague object registration and forwarding colleague object information.
  2. ConcreteMediator role: implement the mediator interface, define a List to manage colleague objects, and coordinate the interaction between the roles of colleague, so it depends on the colleague role;
  3. Abstract colleague class (Colleague) role: define the interface of colleague class, save intermediary object, provide abstract method for colleague object interaction, and realize the common functions of colleague class that affect each other;
  4. Concrete colleague class (ConcreteColleague) role: it is the implementer of the abstract colleague class. When it needs to interact with other colleague objects, the intermediary object is responsible for the subsequent interaction.

Structure chart

Insert picture description here

Instance

The Shaoguan Real Estate Exchange Platform is a platform provided by "real estate intermediary companies" to "seller customers" and "buyer customers" for information exchange. It is more suitable to be implemented in the mode of intermediary.
Insert picture description here

Note: The contents of the article picking in the "software design patterns (Java version)", Author: Cheng fine column

Guess you like

Origin blog.csdn.net/weixin_44048668/article/details/112400754