Home > Backend Development > PHP Tutorial > PHP Design Patterns - Observer Pattern_PHP Tutorial

PHP Design Patterns - Observer Pattern_PHP Tutorial

WBOY
Release: 2016-07-13 09:51:48
Original
752 people have browsed it

PHP Design Pattern - Observer Pattern

Observer pattern (sometimes called publish-subscribe pattern). In this mode, a target object manages all observer objects that depend on it and proactively sends notifications when its own state changes. This is usually accomplished by calling methods provided by each observer. This pattern is often used to implement event handling systems.

UML class diagram:

Important roles:

Abstract Notifier Role (INotifier): Defines the interface rules for notifications.

Concrete Notifier Role (Boss): Implements the abstract notifier interface, and immediately sends notifications to observers upon receiving status changes.

Abstract Observer role (IObserver): Define the operation (Update) interface rules after receiving the notification.

Specific observer role (JingDong): implement specific operation methods.

Core code:

//抽象通知者
abstract class Subject
{
    private $observers = array();

    public function  Attach(Observer $observer)
    {
        array_push($this->observers,$observer);
    }

    public function  Detach(Observer $observer)
    {
        foreach($this->observers as $k=>$v)
        {
            if($v==$observer)
            {
                unset($this->observers[$k]);
            }
        }
    }

    function  Notify()
    {
        foreach($this->observers as $v)
        {
            $v->Update();
        }
    }
}

//具体通知者(Boss和Secretary)
class ConcreteSubject extends Subject
{
   public $subject_state;
}

//抽象观察者
abstract class Observer
{
    public abstract function Update();
}

//具体观察者
class ConcreteObserver extends Observer
{
    private $name;
    private $observerState;
    public $subject;

    public function __construct(ConcreteSubject $_sub,$_name)
    {
        $this->subject = $_sub;
        $this->name = $_name;
    }

    public function  Update()
    {
        $this->observerState = $this->subject->subject_state;
        echo 观察者.$this->name.的新状态是:.$this->observerState.
;
    }
}
Copy after login

Call client code:

header(Content-Type:text/html;charset=utf-8);
//-------------------------观察者模式-----------------------
require_once ./Observe/Observe.php;
//前台
$_s = new ConcreteSubject();

//宝银
$baoyin = new ConcreteObserver($_s, 张三);
$jiangchao = new ConcreteObserver($_s,李四);

//前台记下宝银姜超
$_s->Attach($baoyin);
$_s->Attach($jiangchao);

//前台发现老板回来
$_s->subject_state = 孙总回来了;

//前台发送通知
$_s->Notify();
Copy after login

Advantages:

1. An abstract model has two aspects, one of which depends on the other. Encapsulating these aspects in separate objects allows them to be changed and reused independently.

2. A change in one object will cause one or more other objects to change. It is not known how many objects will change, which can reduce the coupling between objects.

3. An object must notify other objects without knowing who these objects are. It is necessary to create a trigger chain in the system. The behavior of object A will affect object B, and the behavior of object B will affect object C... You can use the observer pattern to create a chain trigger mechanism.




www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1012064.htmlTechArticlePHP design pattern - Observer pattern Observer pattern (sometimes called publish-subscribe pattern). In this mode, a target object manages all observer objects that depend on it,...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template