Observation of PHP design patterns patterns

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

the observer pattern: the definition of a object the multi dependency, when an object changes state, all the objects that depend on it will be informed and updated.

The observer class:

1 Abstract role: thematic role all the observer object reference stored in a set, each subject can be any number of observer. Abstract the theme provided for adding and deleting the observer object interface.

Abstract: 2 observer role defines an interface for all concrete observer, change in the observation update their theme

3 specific topics related to specific roles: the storage state observer object, when the subject specific internal state changes, issued a notice to all registered observers. Specific thematic role usually use a specific subclass implementation.

4 observation of specific roles: store a specific subject object, storage condition, implementation of the abstract observation update interface role required, to make its own state and the theme of the state consistency

Effect.

1 to observe the coupling model of small

2 supports broadcast communication


<?php
//Abstract the theme
interface Subject{
     public function attach($observer);
     public function detach($observer);
     public function notifyObservers();
}
//Specific topics
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;
     }
}
 
//Abstract the observer
interface Observer{
     public function update();
}
//The observer
class ConcreteObserver implement Observer{
     private $_name;
     public function __construct($name){
          $this->_name = $name;
     }
 
     public function update(){
          echo 'Observer',$this->_name.'has notified<br/>';
     }
}
 
//The client
class Client{
     public static function main(){
            $subject = new ConcreteSubject();
            //Add the first observer
            $observer1 = new ConcreteObserver('Martin');
            $subject->attach($observer1);
            //Notice
            $subject->notifyObservers();
 
            //Second new observers
            $observer2 = new ConcreteObserver('jaky');
            $subject->attach($observer2);
            //Notice
            $subject->notifyObservers();
 
            //Remove the observer 1
            $subject->deatch($observer1);
            //Notice
            $subject->notifyObservers();
     }
}
 
Client::main();
?>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Maureen at October 23, 2013 - 7:20 PM