Home >Backend Development >PHP Tutorial >PHP design patterns observer pattern, design pattern observer_PHP tutorial

PHP design patterns observer pattern, design pattern observer_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:58:07867browse

php design pattern observer pattern, design pattern observer

The core of the observer pattern is to separate the client element (observer) from a central class (subject) . Observers need to be notified when the subject knows that an event has occurred. At the same time, we do not want to hardcode the relationship between the subject and the observer. To achieve this, we can allow observers to register on the subject.

The observer pattern implements a low-coupling, non-intrusive notification and update mechanism.

Scenario: When an event occurs, a series of update operations must be performed.

<span>EventGenerator</span>.php
<?<span>php
namespace Baobab;
</span><span>/*</span><span>*
 * Class EventGenerator 基类,事件发生者
 </span><span>*/</span>
<span>abstract</span> <span>class</span><span> EventGenerator{

    </span><span>//</span><span>观察者对于事件发生者是不可见的,事件发生者不关心有哪些观察者关注了事件</span>
    <span>private</span> <span>$observers</span> = <span>array</span><span>();

    </span><span>/*</span><span>*
     * 添加观察者
     </span><span>*/</span>
    <span>function</span> addObserver(Observer <span>$observer</span><span>){
        </span><span>$this</span>->observers[] = <span>$observer</span><span>;
    }

    </span><span>/*</span><span>*
     *逐个通知观察者进行更新的操作
     </span><span>*/</span>
    <span>function</span><span> notify(){
        </span><span>foreach</span>(<span>$this</span>->observers <span>as</span> <span>$observer</span><span>){
            </span><span>$observer</span>-><span>update();
        }
    }
}</span>
<span>Observer</span>.php
<?<span>php
namespace Baobab;
</span><span>/*</span><span>*
 * Class Observer 观察者,观察的对象是事件发生者
 * @package Baobab
 </span><span>*/</span>
<span>interface</span><span> Observer{
    </span><span>/*</span><span>*
     * @param null $event_info  事件的信息
     * 表示事件发生后进行更新的操作
     </span><span>*/</span>
    <span>function</span> update(<span>$event_info</span> = <span>null</span><span>);
}</span>

index.php

<span>class</span> Event <span>extends</span><span> \Baobab\EventGenerator{
    </span><span>/*</span><span>*
     *触发一个新的事件
     </span><span>*/</span>
    <span>function</span><span> trigger(){
        </span><span>echo</span> 'Event发生'<span>;
        </span><span>$this</span>-><span>notify();
    }
}

</span><span>/*</span><span>*
 * 观察者1
 </span><span>*/</span>
<span>class</span> Observer1 <span>implements</span><span> \Baobab\Observer{
    </span><span>function</span> Update(<span>$event_info</span> = <span>null</span><span>){
        </span><span>echo</span> '更新操作代码1'<span>;
    }
}<br />
<span>/*<span>* <br /> * 观察者2 <br /><span> */</span></span></span>
</span><span>class</span> Observer2 <span>implements</span><span> \Baobab\Observer{
    </span><span>function</span> Update(<span>$event_info</span> = <span>null</span><span>){
        </span><span>echo</span> '更新操作代码2'<span>;
    }
}

</span><span>$event</span> = <span>new</span><span> Event();
</span><span>$event</span>->addObserver(<span>new</span><span> Observer1());
</span><span>$event</span>->addObserver(<span>new</span><span> Observer2());
</span><span>$event</span>->trigger();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1105142.htmlTechArticlephp design pattern observer pattern, design pattern observer The core of the observer pattern is to combine customer elements (observers) Separate from a central class (subject). When the subject knows the event...
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