Home  >  Article  >  Backend Development  >  Detailed explanation of the application of observer pattern in PHP design pattern_PHP tutorial

Detailed explanation of the application of observer pattern in PHP design pattern_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:20704browse

Observer pattern: Define a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.
Observer class:
1. Abstract theme role: The theme role saves all references to observer objects in a collection, and each theme can have as many an observer. The abstract theme provides an interface for adding and removing observer objects.
2. Abstract observer role: Define an interface for all specific observers and update themselves when the subject of observation changes
3. Specific topic role: Store relevant states to specific observer objects. When the specific topic When the internal state of the object changes, a notification is sent to all registered observers. Concrete theme roles are usually implemented using a concrete subclass.
4. Specific observer role: Store a specific topic object, store related states, and implement the update interface required by the abstract observer role to make its own state consistent with the state of the topic
Function:
1. The coupling of the observer mode is small
2. Supports broadcast communication

Copy code The code is as follows:

//抽象主题
interface Subject{
     public function attach($observer);
     public function detach($observer);
     public function notifyObservers();
}
//具体主题
class ConcreateSubject implements Subject{
     private $_observers;
     public function __construct(){
          $this->_observers = array();
     }

     public function attach($observer){
          return array_push($this->_observers,$observer);
     }

     public function detach($observer){
          $index = array_search($observer,$this->_observers);
          if($index === false || !array_key_exists($index,$this->_observers)){
               return false;
          }
          unset($this->_observer[$index]);
          return true;
     }

     public function notifyObservers(){
          if(!is_array($this->_observers)){
               return false;
          }
          foreach($this->_observers as $observer){
               $observer->update();
          }
          return true;
     }
}

//抽象观察者
interface Observer{
     public function update();
}
//具体观察者
class ConcreteObserver implement Observer{
     private $_name;
     public function __construct($name){
          $this->_name = $name;
     }

     public function update(){
          echo 'Observer',$this->_name.'has notified
';
     }
}

//客户端
class Client{
     public static function main(){
            $subject = new ConcreteSubject();
            //新增第一个观察者
            $observer1 = new ConcreteObserver('Martin');
            $subject->attach($observer1);
            //通知
            $subject->notifyObservers();

            //新增第二个观察者
            $observer2 = new ConcreteObserver('jaky');
            $subject->attach($observer2);
            //通知
            $subject->notifyObservers();

            //删除观察者1
            $subject->deatch($observer1);
            //通知
            $subject->notifyObservers();
     }
}

Client::main();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327124.htmlTechArticleObserver pattern: Define a one-to-many dependency relationship between objects, when the state of an object changes , all objects that depend on it are notified and updated automatically. Observer class...
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