Home Backend Development PHP Tutorial PHP observer pattern

PHP observer pattern

Nov 21, 2016 pm 05:01 PM
php

Concept

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

When the state of an object changes, it will affect the changes of several other objects. At this time, the observer mode can be used.

The observer pattern complies with the interface isolation principle and achieves loose coupling between objects.

Alias

Publish-Subscribe> Pattern

Model-View Pattern

Source-Listener> Pattern

Slave Pattern

Role

Abstract Subject (Subject): It keeps references to all observer objects in a collection, and each subject can have any number of observers. The abstract theme provides an interface to add and remove observer objects.

Concrete Subject (ConcreteSubject): Store the relevant status in a specific observer object; when the internal status of the specific subject changes, a notification is sent to all registered observers.

Abstract Observer (Observer): Define an interface for all concrete observers and update themselves when notified by the topic.

Concrete Observer: Implement the update interface required by the abstract observer role in order to coordinate its own state with the subject state.

UML diagram

PHP observer pattern

Code

Sample code

The SplSubject and SqlOberver interfaces have been provided in PHP SPL. The source code is as follows:

/**
 * The <b>SplSubject</b> interface is used alongside
 * <b>SplObserver</b> to implement the Observer Design Pattern.
 * @link http://php.net/manual/en/class.splsubject.php
 */
interface SplSubject  {

        /**
         * Attach an SplObserver
         * @link http://php.net/manual/en/splsubject.attach.php
         * @param SplObserver $observer <p>
     * The <b>SplObserver</b> to attach.
         * </p>
         * @return void 
         * @since 5.1.0
         */
        public function attach (SplObserver $observer);

        /**
         * Detach an observer
         * @link http://php.net/manual/en/splsubject.detach.php
         * @param SplObserver $observer <p>
     * The <b>SplObserver</b> to detach.
         * </p>
         * @return void 
         * @since 5.1.0
         */
        public function detach (SplObserver $observer);

        /**
         * Notify an observer
         * @link http://php.net/manual/en/splsubject.notify.php
         * @return void 
         * @since 5.1.0
         */
        public function notify ();

}

/**
 * The <b>SplObserver</b> interface is used alongside
 * <b>SplSubject</b> to implement the Observer Design Pattern.
 * @link http://php.net/manual/en/class.splobserver.php
 */
interface SplObserver  {

        /**
         * Receive update from subject
         * @link http://php.net/manual/en/splobserver.update.php
         * @param SplSubject $subject <p>
     * The <b>SplSubject</b> notifying the observer of an update.
         * </p>
         * @return void 
         * @since 5.1.0
         */
        public function update (SplSubject $subject);

}
Copy after login

Now we write our own code based on these two spl interfaces:

<?php
header(&#39;Content-type:text/html;charset=utf-8&#39;);

/**
 * Class Subject 主题
 */
class Subject implements SplSubject
{
    private $_observers = [];

    /**
     * 实现添加观察者方法
     *
     * @param SplObserver $observer
     */
    public function attach(SplObserver $observer)
    {
        if (!in_array($observer, $this->_observers)) {
            $this->_observers[] = $observer;
        }
    }

    /**
     * 实现移除观察者方法
     *
     * @param SplObserver $observer
     */
    public function detach(SplObserver $observer)
    {
        if (false !== ($index = array_search($observer, $this->_observers))) {
            unset($this->_observers[$index]);
        }
    }

    /**
     * 实现提示信息方法
     */
    public function notify()
    {
        foreach ($this->_observers as $observer) {
            $observer->update($this);
        }
    }

    /**
     * 设置数量
     *
     * @param $count
     */
    public function setCount($count)
    {
        echo "数据量加" . $count . &#39;<br>&#39;;
    }

    /**
     * 设置积分
     *
     * @param $integral
     */
    public function setIntegral($integral)
    {
        echo "积分量加" . $integral . &#39;<br>&#39;;
    }

}

/**
 * Class Observer1 观察者一
 */
class Observer1 implements SplObserver
{
    public function update(SplSubject $subject)
    {
        $subject->setCount(10);
    }
}

/**
 * Class Observer2 观察者二
 */
class Observer2 implements SplObserver
{
    public function update(SplSubject $subject)
    {
        $subject->setIntegral(10);
    }
}


/**
 * Class Client 客户端
 */
class Client
{
    /**
     * 测试方法
     */
    public static function test()
    {
        // 初始化主题
        $subject = new Subject();
        // 初始化观察者一
        $observer1 = new Observer1();
        // 初始化观察者二
        $observer2 = new Observer2();
        // 添加观察者一
        $subject->attach($observer1);
        // 添加观察者二
        $subject->attach($observer2);
        // 消息提示
        $subject->notify();//输出:数据量加1 积分量加10
        // 移除观察者一
        $subject->detach($observer1);
        // 消息提示
        $subject->notify();//输出:数据量加1 积分量加10 积分量加10
    }
}

// 执行测试
Client::test();
Copy after login

Running results

The amount of data plus 10
The amount of points plus 10
The amount of points plus 10

Advantages and disadvantages

Advantages

The coupling between the observer and the subject is small;

Supports broadcast communication;

Disadvantages

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

Applicable scenarios

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

When changes to one object require changes to other objects at the same time, 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.


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles