Observer mode: mainly involves two objects, the subject (observed) and the observer. If the topic changes, all observers subscribed to the topic are notified.

Example: There is a generic observer pattern template in the JDK. Observable and Observer. Their relationship is as follows:

Topics can be subscribed to by many observers, so topics have a one-to-many relationship with observers, and observers can hold references to topics.

Observables class:

Including: register observer (addObserver), remove observer (deleteObserver), notifyObserver (observer) and other methods.

The Observer interface:

There is only one update method that receives the updated content after the topic has been updated.

As long as you inherit the Observerable class and implement the Observer interface, you can implement a simple Observer pattern. However, this is not very friendly, because the observer receives updates of type Object and is forced to type. So the update method can be optimized by introducing generics.

Model analysis:

1. The roles and responsibilities are clearly divided, and the subject is responsible for managing the observer and notifying the observer once the subject content changes; The observer is responsible for receiving updates.