Home >Common Problem >What are the applicable scenarios for the observer pattern?

What are the applicable scenarios for the observer pattern?

藏色散人
藏色散人Original
2020-07-01 10:30:056428browse

Applicable scenarios for the observer pattern include: 1. When an abstract model has two aspects, one of which depends on the other; 2. When changes to one object require changes to other objects at the same time, without Know how many objects need to be changed; 3. When an object must notify other objects, it cannot assume who the other objects are.

What are the applicable scenarios for the observer pattern?

Observer Pattern

Defines a one-to-many dependency relationship between objects. When an object's state changes, all objects that depend on it are notified and automatically updated

Applicability

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

  • When changes to one object require changes to other objects at the same time, and you do not know the specific How many objects are left to change

  • When an object must notify other objects, but it cannot assume who the other objects are. In other words, you don’t know that these objects are tightly coupled

For more related knowledge, please visit PHP中文网!

The above is the detailed content of What are the applicable scenarios for 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