Application of the observer mode of php design pattern

  • 2020-06-01 09:19:14
  • OfStack

Observer mode: defines a 1-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.
Observer class:
1. Abstract topic role: the topic role stores all references to observer objects in a collection, and each topic can have any number of observers. Abstract topics provide interfaces to add and remove observer objects.
2. Abstract observer role: define an interface for all concrete observers and update yourself when the topic of observation changes
3. Specific topic role: stores the relevant state to the specific observer object, and notifies all registered observers when the internal state of the specific topic changes. A specific topic role is typically implemented with one concrete subclass.
4. Specific observer role: store a specific topic object, store the relevant state, and realize the update interface required by the abstract observer role, so that its own state and the state of the topic remain 1
Function:
1. The coupling degree of observer mode is small
2. Support broadcast communication

<?php
// Abstract subject 
interface Subject{
     public function attach($observer);
     public function detach($observer);
     public function notifyObservers();
}
// The specific topic 
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 observer 
interface Observer{
     public function update();
}
// Concrete 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();
            // The first new 1 An observer 
            $observer1 = new ConcreteObserver('Martin');
            $subject->attach($observer1);
            // notice 
            $subject->notifyObservers();

            // The first new 2 An observer 
            $observer2 = new ConcreteObserver('jaky');
            $subject->attach($observer2);
            // notice 
            $subject->notifyObservers();

            // Delete observer 1
            $subject->deatch($observer1);
            // notice 
            $subject->notifyObservers();
     }
}

Client::main();
?>

Related articles: