Home  >  Article  >  Backend Development  >  How does the observer pattern work in PHP?

How does the observer pattern work in PHP?

WBOY
WBOYOriginal
2024-06-03 14:09:02242browse

The Observer pattern in PHP is a design pattern that allows observers to subscribe to and respond to state changes in a topic. When the topic state changes, it notifies observers, who can update themselves accordingly: the topic class maintains a list of observers and provides methods to attach and remove observers. The topic class provides methods for getting and setting state, and triggers observer notifications when the state changes. The observer class defines an update method that is called when the subject state changes. Concrete observer classes implement update methods to update their own logic. In practice, observers subscribe to topics and when the topic state changes, the observer is notified and updates itself.

How does the observer pattern work in PHP?

How the observer pattern works in PHP

Introduction

Observer pattern Is a software design pattern that allows objects (called observers) to subscribe to and respond to state changes of other objects (called topics). When the topic's state changes, it notifies all observers, and observers can update themselves accordingly.

Code structure

In PHP, we can use the following class structure to implement the observer pattern:

Theme class:

class Subject
{
    private $observers = [];
    private $state;

    public function attach(Observer $observer)
    {
        $this->observers[] = $observer;
    }

    public function detach(Observer $observer)
    {
        $index = array_search($observer, $this->observers);
        if ($index !== false) {
            unset($this->observers[$index]);
        }
    }

    public function getState()
    {
        return $this->state;
    }

    public function setState($state)
    {
        $this->state = $state;
        $this->notifyObservers();
    }

    private function notifyObservers()
    {
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }
}

Observer class:

interface Observer
{
    public function update(Subject $subject);
}

Specific observer class:

class ConcreteObserver1 implements Observer
{
    public function update(Subject $subject)
    {
        // 更新自己的逻辑
    }
}

class ConcreteObserver2 implements Observer
{
    public function update(Subject $subject)
    {
        // 更新自己的逻辑
    }
}

Actual case

We can use the following code example to illustrate the application of the observer pattern in practice:

$subject = new Subject();

$observer1 = new ConcreteObserver1();
$observer2 = new ConcreteObserver2();

$subject->attach($observer1);
$subject->attach($observer2);

$subject->setState('New state'); // 触发更新

// 观察者收到更新并相应更新自己

When the status of a topic is updated, all observers subscribed to it will be notified and respond accordingly Update yourself.

The above is the detailed content of How does the observer pattern work in PHP?. 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