Home >Common Problem >What is the observer pattern?

What is the observer pattern?

Guanhui
GuanhuiOriginal
2020-06-28 16:12:294432browse

The observer pattern is an object behavior pattern that defines a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and are automatically updated, thereby improving application maintainability and reusability.

What is the observer pattern?

#The observer pattern is an object behavior pattern. It defines a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated. In the observer pattern, the topic is the publisher of notifications. When it issues notifications, it does not need to know who its observers are. Any number of observers can subscribe and receive notifications. The observer pattern is not only widely used in the interaction between software interface elements, but also in the interaction between business objects, permission management, etc.

The Observer pattern (Observer) perfectly separates the observer from the observed object. For example, the user interface can serve as an observer, and the business data is the observed. The user interface observes changes in the business data, and after discovering changes in the data, it is displayed on the interface. One principle of object-oriented design is that each class in the system will focus on a certain function rather than other aspects. An object does one thing and does it well. The Observer pattern draws clear boundaries between modules, improving the maintainability and reusability of applications.

The observer design pattern defines a one-to-many combination relationship between objects, so that when the state of an object changes, all objects that depend on it are notified and automatically refreshed.

How to implement the Observer pattern

There are many ways to implement the Observer pattern. Fundamentally speaking, the pattern must contain two roles: the observer and the observed object. . In the example just now, the business data is the observed object and the user interface is the observer. There is a logical relationship of "observation" between the observer and the observed. When the observed changes, the observer will observe such changes and respond accordingly. If such an observation process is used between the user interface and business data, it can ensure a clear boundary between the interface and the data. Assuming that the requirements of the application change and the interface performance needs to be modified, only a user interface and business data need to be rebuilt. No changes need to occur.

Observer pattern usage scenarios

1. When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in separate objects so that they can be changed and reused independently.

2. When changes to one object require changes to other objects at the same time, it is not known how many objects need to be changed.

3. When an object must notify other objects, it cannot assume who the other objects are. In other words, you don't want these objects to be tightly coupled.

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of What is the observer pattern?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does kafka mean?Next article:What does kafka mean?