PHP Design Patterns--Observer Pattern

  Observer mode (Observer) is a behavioral mode that is used a lot. There are mainly two objects: the observer and the observed (target), and the observer changes accordingly according to the change of the target.

 Mentioning this mode reminds me of several popular games in the past two years, such as: King of Glory 5V5 battle, when your teammates are attacked, careful people will find that your teammates' small avatars will keep turning red , until the countdown turns gray, and at the end there will be a broadcast message that your teammate has been killed. 

 In this process, the attacked teammate is the target, and you and other teammates are the observers. After receiving the signal that the teammate was attacked, they quickly rushed to support. 

 In the whole process, the observer can be three or five people, which is very convenient to expand without affecting each other.

<?php  
  
// abstract center  
abstract Class Central   
{  
    /**
     * Observer collection
     * @var array
     */  
    private $observers = array();  
  
    /**
     * add observer
     * @access public  
     * @param object $observer observer
     */  
    public function Attach(Observer $observer)  
    {  
        array_push($this->observers,$observer);  
    }  
  
    /**
     * notification function
     * @access public  
     */  
    function Notify()  
    {  
        foreach ($this->observers as $k => $v) {  
            $v->Update();  
        }  
    }  
}  
  
// control center  
Class controlCentral extends Central  
{  
    /**
     * Notification information
     * @var array
     */  
    public $message;  
}  
  
  
abstract Class Observer  
{  
    /**
     * notification function
     * @access public  
     */  
    public abstract function Update();  
}  
Class concreteObserver extends Observer  
{  
    /**
     * Observer
     * @var string
     */  
    private $name;  
  
    /**
     * Observe status
     * @var string
     */  
    private $observerState;  
  
    /**
     * control center
     * @var object
     */  
    public $central ;  
  
    /**
     * Constructor
     * @access public  
     * @param object $_cen control center
     * @param object $_name observer
     */  
    function __construct(controlCentral $_cen,$_name)  
    {  
        $this->name = $_name;  
        $this->central = $_cen;  
    }  
  
    /**
     * Update notification information
     * @access public  
     */  
    function Update()  
    {  
        $this->observerState = $this->central->message;  
        echo $this->name."发现:".$this->observerState."<br/>";  
    }  
}  

<?php  
  
// Observer pattern  
  
header("Content-Type:text/html;charset=utf-8");    
require_once "Observe.php";  
  
// instance observer center  
$central = new controlCentral();  
// notification information  
$central->message = "The knight is being attacked";  
  
// specific fighter  
$zs = new concreteObserver($central,"Warrior");  
// specific priest  
$ms = new concreteObserver($central,"Priest");  
  
// add observer to control center  
$central->Attach($zs);  
$central->Attach($ms);  
  
// notify  
$central->Notify();  

Output result:

Warrior found: Knight is being attacked
Priest found: Knight is being attacked

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613141&siteId=291194637