-
- //Observed abstract class
- class Observed implements SplSubject{
- protected $_name;
- protected $_observers;
- //Instantiation, generate observer object
- public function __construct(){
- $this-> ;_observers = new SplObjectStorage();
- }
- //Add observer object
- public function attach(SplObserver $observer){
- $this->_observers->attach($observer);
- }
- //Delete observer Object
- public function detach(SplObserver $observer){
- $this->_observers->detach($observer);
- }
- //Notification message
- public function notify(){
- foreach($this-> _observers as $observer){
- $observer->showMessage($this);
- }
- }
- //Normal method: Set value
- public function setName($name){
- $this->_name = $name;
- $this->notify();
- }
- //Normal method: Get value
- public function getName(){
- return $this->_name;
- }
- //Normal method: Set age
- public function setAge ($age){
- $this->age = $age;
- foreach($this->_observers as $observer){
- $observer->showAge($this->_name,$this-> age);
- }
- }
- }
- //Observer abstract class
- class Observer implements SplObserver{
- //Show message prompt
- public function showMessage(SplSubject $obj){
- $user = $obj->getName() ;
- if($user==='admin'){
- echo 'Hello, ',$user,'Welcome to the management background
';
- }else{
- echo "Hello, ' $user' You have been added to the user list
";
- }
- }
- //This is an abstract method inherited from the parent class
- public function update(SplSubject $subject) {}
- //Display personal age
- public function showAge($name,$age){
- echo "<script>alert('$name's age is: $age')</script>";
- }
- }
- $subject = new Observed( ); //Generate an observed object
- $observer = new Observer(); //Generate an observer object
- $subject->attach($observer);//Pass the observer into the observed
- $subject->setName('Zhang San'); //Call the setName method
- /*
- * By calling $this->notify();
- * By calling $this->notify () will call the $observer->showMessage($this) method,
- * that is, the showMessage($obj) method of each observer object;
- */
- $subject->setName('admin');
- $subject->setAge(24);
Copy code
Example 2, observer mode:
-
- interface Subject
- {
- public function Attach($Observer); //Add an observer
- public function Detach($Observer); //Kick out the observer
- public function Notify(); //Meet the conditions Notify observers when
- public function SubjectState($Subject); //Observation conditions
- }
- class Boss Implements Subject
- {
- public $_action;
- private $_Observer;
- public function Attach($Observer)
- {
- $this- >_Observer[] = $Observer;
- }
- public function Detach($Observer)
- {
- $ObserverKey = array_search($Observer, $this->_Observer);
- if($ObserverKey !== false)
- {
- unset($this->_Observer[$ObserverKey]);
- }
- }
- public function Notify()
- {
- foreach($this->_Observer as $value)
- {
- $value->Update( );
- }
- }
- public function SubjectState($Subject)
- {
- $this->_action = $Subject;
- }
- }
- abstract class Observer
- {
- protected $_UserName;
- protected $_Sub; __construct($Name,$Sub)
- {
- $this->_UserName = $Name;
- $this->_Sub = $Sub;
- }
- public abstract function Update(); //Receive the method
- } / /bbs.it-home.org
- class StockObserver extends Observer
- {
- public function __construct($name,$sub)
- {
- parent::__construct($name,$sub);
- }
- public function Update()
- {
- echo $this->_Sub->_action.$this->_UserName." Run quickly...";
- }
- }
- $huhansan = new Boss(); //Observed person
- $ gongshil = new StockObserver("三毛",$huhansan); //Initialize the observer
- $huhansan->Attach($gongshil); //Add an observer
- $huhansan->Attach($gongshil); / /Add an identical observer
- $huhansan->Detach($gongshil); //Kick out an observer in the base
- $huhansan->SubjectState("The police are coming"); //Meet the conditions that are met
- $huhansan->Notify(); //Copy code
-
through all valid observers
Instructions:
A class (let's call it an Observable) that inherits the interface of an Observable container
The methods in this interface include: add observer, kick out observer, remind observer, and observation conditions
Adding an observer is equivalent to registering some classes into this container. Of course, the premise of these classes is that they need to have a method that responds to the observed information.
The observed container accepts different observation condition parameters to determine whether it needs to remind all registered observers in the container to respond accordingly.
Once the reminder conditions are met, all observers in the container are reminded to call the corresponding methods and implement changes to the corresponding observer instances.
|