What is the Observer pattern? For example, as you book a newspaper, like you want to know what's happening in the United States recently, you might subscribe to an American weekly, and once the United States has a new story, the American Weekly will issue it and mail it to you, and when you receive it, you will be able to understand the latest developments in the United States. In fact, this is the observer pattern, a is interested in the change of B, the observer registered as B, when B changes, notify A, tell B to change. This is a very typical use of the observer, and I call this method the classic observer pattern. There is, of course, another observer pattern------the generalized observer pattern.
From a classical point of view, the Observer pattern is a mode of notification change, which is generally considered useful only in situations where the object is interested in change. The subject object knows that there is an observer, and the setting maintains a queue for the observer, and in a broad sense, the observer pattern is a pattern that passes the change data, a mode to be used when looking at the object's properties, and the subject object does not know the observer's presence, more like a bystander. You need to know the state of the subject object, so even if the subject object does not change, the observer may go back to the subject object. In other words, a generalized observer pattern is a pattern of passing data between different objects.
The observer pattern should be one of the most widely used design patterns in object-oriented programming, and from a methodological standpoint, the traditional cognitive theory holds that the world is made up of objects, and we can understand the nature of objects through constant observation and understanding. The whole human cognitive scale is based on the behavior of "observing". We understand the world by constantly interacting with other objects in the world and observing them. Similarly, in the world of programs, every instance we build is also done by observing changes in other instances and responding to them. This is why the Observer pattern is presented separately, to make a special analysis of the reasons---in my opinion he is a lot of other design patterns of the basic pattern, and is a very important design pattern in programming.
Classic Observer pattern
The classic observer pattern is considered to be the behavior pattern of the object, also called the Publish Subscription (Publish/subscribe) mode, the model-view (models, view) mode, the source-listener (Source/listener) mode, or the slave mode. The classic observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. When the subject object changes in state, all observer objects are notified so that they can automatically update themselves to make some corresponding actions in their lives. Examples from the beginning of the article are the application of typical observer patterns.
Viewer mode for iOS design mode