Home > Backend Development > PHP Tutorial > Simple example of php observer pattern

Simple example of php observer pattern

WBOY
Release: 2016-07-25 08:52:06
Original
1074 people have browsed it
  1. //Observed abstract class
  2. class Observed implements SplSubject{
  3. protected $_name;
  4. protected $_observers;
  5. //Instantiation, generate observer object
  6. public function __construct(){
  7. $this-> ;_observers = new SplObjectStorage();
  8. }
  9. //Add observer object
  10. public function attach(SplObserver $observer){
  11. $this->_observers->attach($observer);
  12. }
  13. //Delete observer Object
  14. public function detach(SplObserver $observer){
  15. $this->_observers->detach($observer);
  16. }
  17. //Notification message
  18. public function notify(){
  19. foreach($this-> _observers as $observer){
  20. $observer->showMessage($this);
  21. }
  22. }
  23. //Normal method: Set value
  24. public function setName($name){
  25. $this->_name = $name;
  26. $this->notify();
  27. }
  28. //Normal method: Get value
  29. public function getName(){
  30. return $this->_name;
  31. }
  32. //Normal method: Set age
  33. public function setAge ($age){
  34. $this->age = $age;
  35. foreach($this->_observers as $observer){
  36. $observer->showAge($this->_name,$this-> age);
  37. }
  38. }
  39. }
  40. //Observer abstract class
  41. class Observer implements SplObserver{
  42. //Show message prompt
  43. public function showMessage(SplSubject $obj){
  44. $user = $obj->getName() ;
  45. if($user==='admin'){
  46. echo 'Hello, ',$user,'Welcome to the management background
    ';
  47. }else{
  48. echo "Hello, ' $user' You have been added to the user list
    ";
  49. }
  50. }
  51. //This is an abstract method inherited from the parent class
  52. public function update(SplSubject $subject) {}
  53. //Display personal age
  54. public function showAge($name,$age){
  55. echo "<script>alert('$name's age is: $age')</script>";
  56. }
  57. }
  58. $subject = new Observed( ); //Generate an observed object
  59. $observer = new Observer(); //Generate an observer object
  60. $subject->attach($observer);//Pass the observer into the observed
  61. $subject->setName('Zhang San'); //Call the setName method
  62. /*
  63. * By calling $this->notify();
  64. * By calling $this->notify () will call the $observer->showMessage($this) method,
  65. * that is, the showMessage($obj) method of each observer object;
  66. */
  67. $subject->setName('admin');
  68. $subject->setAge(24);
Copy code

Example 2, observer mode:

  1. interface Subject
  2. {
  3. public function Attach($Observer); //Add an observer
  4. public function Detach($Observer); //Kick out the observer
  5. public function Notify(); //Meet the conditions Notify observers when
  6. public function SubjectState($Subject); //Observation conditions
  7. }
  8. class Boss Implements Subject
  9. {
  10. public $_action;
  11. private $_Observer;
  12. public function Attach($Observer)
  13. {
  14. $this- >_Observer[] = $Observer;
  15. }
  16. public function Detach($Observer)
  17. {
  18. $ObserverKey = array_search($Observer, $this->_Observer);
  19. if($ObserverKey !== false)
  20. {
  21. unset($this->_Observer[$ObserverKey]);
  22. }
  23. }
  24. public function Notify()
  25. {
  26. foreach($this->_Observer as $value)
  27. {
  28. $value->Update( );
  29. }
  30. }
  31. public function SubjectState($Subject)
  32. {
  33. $this->_action = $Subject;
  34. }
  35. }
  36. abstract class Observer
  37. {
  38. protected $_UserName;
  39. protected $_Sub; __construct($Name,$Sub)
  40. {
  41. $this->_UserName = $Name;
  42. $this->_Sub = $Sub;
  43. }
  44. public abstract function Update(); //Receive the method
  45. } / /bbs.it-home.org
  46. class StockObserver extends Observer
  47. {
  48. public function __construct($name,$sub)
  49. {
  50. parent::__construct($name,$sub);
  51. }
  52. public function Update()
  53. {
  54. echo $this->_Sub->_action.$this->_UserName." Run quickly...";
  55. }
  56. }
  57. $huhansan = new Boss(); //Observed person
  58. $ gongshil = new StockObserver("三毛",$huhansan); //Initialize the observer
  59. $huhansan->Attach($gongshil); //Add an observer
  60. $huhansan->Attach($gongshil); / /Add an identical observer
  61. $huhansan->Detach($gongshil); //Kick out an observer in the base
  62. $huhansan->SubjectState("The police are coming"); //Meet the conditions that are met
  63. $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.



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template