首頁 > 後端開發 > php教程 > 观察者模式,观察者模式类图_PHP教程

观察者模式,观察者模式类图_PHP教程

WBOY
發布: 2016-07-13 09:57:21
原創
1223 人瀏覽過

观察者模式,观察者模式类图

观察者模式

<?<span>php
    </span><span>//</span><span>定义观察者调用接口</span>
    <span>class</span><span> transfer{
        </span><span>protected</span> <span>$_observers</span> = <span>array</span><span>();
        
        </span><span>//</span><span>注册对象</span>
        <span>public</span> <span>function</span> register(<span>$sub</span><span>){
            </span><span>$this</span>->_observers[] = <span>$sub</span><span>;    
        }
        
        </span><span>//</span><span>外部统一调用</span>
        <span>public</span> <span>function</span><span> trigger(){
            </span><span>if</span>(!<span>empty</span>(<span>$this</span>-><span>_observers)){
                </span><span>foreach</span>(<span>$this</span>->_observers <span>as</span> <span>$observer</span><span>){
                    </span><span>$observer</span>-><span>update();
                }    
            }
        }
    }
    
    </span><span>//</span><span>观察者接口</span>
    <span>interface</span><span> obserable{
        </span><span>public</span> <span>function</span><span> update();
    }
    
    </span><span>//</span><span>实现观察者</span>
    <span>class</span> listen <span>implements</span><span> obserable{
        </span><span>public</span> <span>function</span><span> update(){
            </span><span>echo</span> 'now first time you need to do listen<br/>'<span>;
        }
    }
    
    </span><span>class</span> read <span>implements</span><span> obserable{
        </span><span>public</span> <span>function</span><span> update(){
            </span><span>echo</span> 'now first time you need to read<br/>'<span>;
        }
    }
    
    </span><span>class</span> speak <span>implements</span><span> obserable{
        </span><span>public</span> <span>function</span><span> update(){
            </span><span>echo</span> 'now first time you need to speak<br/>'<span>;
        }
    }
    
    </span><span>class</span> write <span>implements</span><span> obserable{
        </span><span>public</span> <span>function</span><span> update(){
            </span><span>echo</span> 'now first time you need to write<br/>'<span>;
        }
    }
    
    </span><span>$transfer</span> = <span>new</span><span> transfer();
    </span><span>$transfer</span>->register(<span>new</span><span> listen());
    </span><span>$transfer</span>->register(<span>new</span><span> read());
    </span><span>$transfer</span>->register(<span>new</span><span> speak());
    </span><span>$transfer</span>->register(<span>new</span><span> write());
    </span><span>$transfer</span>-><span>trigger();
    
    
    
    
    
    
    
    
    
    
    
    
    </span>
登入後複製

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/983246.htmlTechArticle观察者模式,观察者模式类图 观察者模式 ? php // 定义观察者调用接口 class transfer{ protected $_observers = array (); // 注册对象 public function regis...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板