Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Observer pattern in PHP Design pattern

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the Observer pattern in the PHP Design pattern". In the daily operation, I believe many people have doubts about how to understand the Observer pattern in the PHP Design pattern. The editor consulted all kinds of materials and sorted out the simple and useful operation methods. I hope it will be helpful for you to answer the doubts about "how to understand the Observer pattern in the PHP Design pattern". Next, please follow the editor to study!

[intention]

Defines an one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated [GOF95], also known as publish-subscribe (Publish-Subscribe) mode, model-view (Model-View) pattern, source-monitor (Source-Listener) mode, or Dependents mode.

[observer pattern structure diagram]

[main role in observer mode]

1. Abstract topic (Subject) role: the topic role stores all references to the observer object in a collection, and each topic can have as many observers as you want. Abstract topics provide an interface to add and delete observer objects.

two。 Abstract observer (Observer) role: define an interface for all concrete observers to update themselves when the subject of observation changes.

3. Specific topic (ConcreteSubject) role: stores the relevant state to a specific observer object, and notifies all registered observers when the internal state of a specific topic changes. Specific topic roles are usually implemented with a concrete subclass.

4. Concrete ConcretedObserver role: stores a specific topic object, stores related states, and implements the update interface required by the abstract observer role to make its own state consistent with the state of the topic.

[advantages and disadvantages of the observer model]

Advantages of the observer model:

1. There is less coupling between the observer and the subject.

two。 Support for broadcast communication

Disadvantages of the observer model:

Since the observer is not aware of the existence of other observers, it may know nothing about the ultimate cost of changing the goal. This may cause unexpected updates.

[observer mode applicable scenario]

When an abstract model has two aspects, one aspect depends on the other.

When you change one object, you need to change other objects at the same time, and you don't know how many objects need to be changed.

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.

[observer mode and other modes]

1. Intermediary pattern (Mediator): by encapsulating complex update semantics, ChangeManager acts as an intermediary between the target and the observer.

two。 Singleton mode (singleton mode): ChangeManager can use Singleton mode to ensure that it is unique and globally accessible.

[observer mode PHP example]

The copy code is as follows:

At this point, the study on "how to understand the Observer pattern in the PHP design pattern" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report