首頁 >php教程 >php手册 >PHP 高级编程(3)

PHP 高级编程(3)

WBOY
WBOY原創
2016-06-13 09:33:231201瀏覽

SPL(标准PHP库 - Standard PHP Library)是PHP5面向对象功能中重要的部分。原文解释是这样的“The Standard PHP Library (SPL) is a collection of interfaces and classes that are meant to solve common problems”。

SplSubject 和 SplObserver 接口

The SplSubject interface is used alongside SplObserver to implement the Observer Design Pattern.

观察者模式是一种简单的事件系统,包含了两个或更多的互相交互的类。这一模式允许某个类观察另一个类的状态,当被观察类的状态发生变化时,这个模式会得到通知。被观察的类叫subject,负责观察的类叫做Observer 。PHP 提供的 SplSubject 和 SplObserver接口可用来表达这些内容。

<span>SplSubject {
</span><span>/*</span><span> 方法 </span><span>*/</span>
<span>abstract</span> <span>public</span> <span>void</span><span> attach ( SplObserver $observer )
</span><span>abstract</span> <span>public</span> <span>void</span><span> detach ( SplObserver $observer )
</span><span>abstract</span> <span>public</span> <span>void</span> notify ( <span>void</span><span> )
}</span>
<span>SplObserver {
</span><span>/*</span><span> 方法 </span><span>*/</span>
<span>abstract</span> <span>public</span> <span>void</span><span> update ( SplSubject $subject )
}</span>

这里,splsubject类维护了一个特定状态,当这个状态发生变化时,他就会调用notify方法,所以之前使用attach注册的splobserver实例的update就会被调用。这里我们实现一个简单地观察者模式的例子

<?<span>php
</span><span>/*</span><span>*
 * Subject,that who makes news
 </span><span>*/</span>
<span>class</span><span> Newspaper implements \SplSubject{
    </span><span>private</span><span> $name;
    </span><span>private</span> $observers =<span> array();
    </span><span>private</span><span> $content;
    
    </span><span>public</span><span> function __construct($name) {
        $</span><span>this</span>->name =<span> $name;
    }

    </span><span>//</span><span>add observer</span>
    <span>public</span><span> function attach(\SplObserver $observer) {
        $</span><span>this</span>->observers[] =<span> $observer;
    }
    
    </span><span>//</span><span>remove observer</span>
    <span>public</span><span> function detach(\SplObserver $observer) {
        
        $key </span>= array_search($observer,$<span>this</span>->observers, <span>true</span><span>);
        </span><span>if</span><span>($key){
            unset($</span><span>this</span>-><span>observers[$key]);
        }
    }
    
    </span><span>//</span><span>set breakouts news</span>
    <span>public</span><span> function breakOutNews($content) {
        $</span><span>this</span>->content =<span> $content;
        $</span><span>this</span>-><span>notify();
    }
    
    </span><span>public</span><span> function getContent() {
        </span><span>return</span> $<span>this</span>->content.<span>"</span><span> ({$this->name})</span><span>"</span><span>;
    }
    
    </span><span>//</span><span>notify observers(or some of them)</span>
    <span>public</span><span> function notify() {
        </span><span>foreach</span> ($<span>this</span>->observers <span>as</span><span> $value) {
            $value</span>->update($<span>this</span><span>);
        }
    }
}

</span><span>/*</span><span>*
 * Observer,that who recieves news
 </span><span>*/</span>
<span>class</span><span> Reader implements SplObserver{
    </span><span>private</span><span> $name;
    
    </span><span>public</span><span> function __construct($name) {
        $</span><span>this</span>->name =<span> $name;
    }
    
    </span><span>public</span><span> function update(\SplSubject $subject) {
        echo $</span><span>this</span>->name.<span>'</span><span> is reading breakout news <b></span><span>'</span>.$subject->getContent().<span>'</span><span></b><br></span><span>'</span><span>;
    }
}

$newspaper </span>= <span>new</span> Newspaper(<span>'</span><span>Newyork Times</span><span>'</span><span>);

$allen </span>= <span>new</span> Reader(<span>'</span><span>Allen</span><span>'</span><span>);
$jim </span>= <span>new</span> Reader(<span>'</span><span>Jim</span><span>'</span><span>);
$linda </span>= <span>new</span> Reader(<span>'</span><span>Linda</span><span>'</span><span>);

</span><span>//</span><span>add reader</span>
$newspaper-><span>attach($allen);
$newspaper</span>-><span>attach($jim);
$newspaper</span>-><span>attach($linda);

</span><span>//</span><span>remove reader</span>
$newspaper-><span>detach($linda);

</span><span>//</span><span>set break outs</span>
$newspaper->breakOutNews(<span>'</span><span>USA break down!</span><span>'</span><span>);

</span><span>//</span><span>=====output======
</span><span>//</span><span>Allen is reading breakout news USA break down! (Newyork Times)
</span><span>//</span><span>Jim is reading breakout news USA break down! (Newyork Times)</span>

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn