Design Patterns————Observer Pattern

The factory pattern is divided into simple factory, factory and abstract factory. The implementation of the three factories is more and more complicated.

Observer pattern

      It is essentially a subscription/publishing model, which is logically a one-to-many dependency .

      What does that mean? It is like a group of guards staring at a prisoner. As long as the prisoner has a change, the guard must take action immediately (it may also be an update state, which is essentially an action), then the guard is the observer, and the prisoner is the observed.

      solved problem:

      In the system, when implementing this one-to-many logic with a certain relationship between them, since it is necessary to maintain the cooperative relationship between them, the easiest way is to use tight coupling. But this increases the difficulty of development, and it is easy to cause bugs

     The observer pattern solves such a problem, while keeping a series of observers and observed objects working together , while decoupling between them.

    

Intent: Define a one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.

The main solution: the problem of notifying other objects of changes in the state of an object, and taking into account ease of use and low coupling to ensure a high degree of collaboration.

When to use: When the state of an object (target object) changes, all dependent objects (observer objects) will be notified and broadcast notification.

How to fix: Using object-oriented techniques, this dependency can be weakened.

Key code: There is an ArrayList in the abstract class to store the observers.

Application example:  1. A certain product on JD.com is temporarily out of stock, prompting the user to pay attention to the arrival notification. This temporarily out of stock product is the observer, and the user who clicks and follows this product is the observer. 2. The teacher regularly sends the latest test question analysis emails to students with scores below 60 points. Students who fail each round of the test will be automatically removed from the mailing list. will be added to the mailing list.

Advantages:  1. The observer and the observed are abstractly coupled. 2. Establish a trigger mechanism.

Disadvantages:  1. If an observed object has many direct and indirect observers, it will take a lot of time to notify all observers. 2. If there is a circular dependency between the observer and the observation target, the observation target will trigger a cyclic call between them, which may cause the system to crash. 3. The observer mode has no corresponding mechanism to let the observer know how the observed target object has changed, but only know that the observed target has changed.

Usage scenarios:  1. There are methods common to multiple subclasses, and the logic is the same. 2. Important and complex methods can be considered as template methods.

Guess you like

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