欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實現(xiàn)的觀察者模式實例

 更新時間:2017年06月21日 11:16:52   作者:北京流浪兒  
這篇文章主要介紹了PHP實現(xiàn)的觀察者模式,結(jié)合具體實例形式分析了php觀察者模式的定義與使用方法,需要的朋友可以參考下

本文實例講述了PHP實現(xiàn)的觀察者模式。分享給大家供大家參考,具體如下:

<?php
  //定義觀察者調(diào)用接口
  class transfer{
    protected $_observers = array();
    //注冊對象
    public function register($sub){
      $this->_observers[] = $sub;
    }
    //外部統(tǒng)一調(diào)用
    public function trigger(){
      if(!empty($this->_observers)){
        foreach($this->_observers as $observer){
          $observer->update();
        }
      }
    }
  }
  //觀察者接口
  interface obserable{
    public function update();
  }
  //實現(xiàn)觀察者
  class listen implements obserable{
    public function update(){
      echo 'now first time you need to do listen<br/>';
    }
  }
  class read implements obserable{
    public function update(){
      echo 'now first time you need to read<br/>';
    }
  }
  class speak implements obserable{
    public function update(){
      echo 'now first time you need to speak<br/>';
    }
  }
  class write implements obserable{
    public function update(){
      echo 'now first time you need to write<br/>';
    }
  }
  $transfer = new transfer();
  $transfer->register(new listen());
  $transfer->register(new read());
  $transfer->register(new speak());
  $transfer->register(new write());
  $transfer->trigger();

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論