Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Observer pattern in php Design pattern

Shulou Source: shulou.com Published: 2022-06-03 05:57:37 11月17日 Update

This article will explain in detail how to use the Observer pattern in the php design pattern. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Observer pattern: defines an one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and updated automatically.

Observer category:

1. Abstract topic 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 role: define an interface for all concrete observers to update themselves when the subject of observation changes

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

4. Specific observer role: store a specific topic object, store related states, and implement the update interface required by the abstract observer role to make its own state consistent with the state of the topic

Function:

1. The coupling degree of observer mode is small.

two。 Support for broadcast communication

The copy code is as follows:

This is the end of this article on "how to use the Observer pattern in php Design pattern". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Observation observer subject object role pattern state interface article storage update design pattern design code more good practical consistent function content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS Docker Shulou Information Apple