Guest User

Untitled

a guest
Oct 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. <?php
  2. require_once 'observer.php';
  3. require_once 'subject.php';
  4.  
  5. use Common\Observer\Observer;
  6. use Common\Observer\Subject;
  7.  
  8. class Notice
  9. {
  10. use Subject;
  11.  
  12. /**
  13. * The title
  14. * @var string
  15. */
  16. private $title = '';
  17.  
  18. /**
  19. * The notice content
  20. * @var unknown_type
  21. */
  22. private $content = '';
  23.  
  24. /**
  25. * The last modified time
  26. * @var unknown_type
  27. */
  28. private $lastModified = null;
  29.  
  30. /**
  31. * The constructor
  32. */
  33. public function __construct($title, $content)
  34. {
  35. $this->title = $title;
  36. $this->content = $content;
  37. $this->lastModified = new DateTime();
  38. }
  39.  
  40. public function __get($attribute)
  41. {
  42. return $this->$attribute;
  43. }
  44.  
  45. public function __set($attribute, $value)
  46. {
  47. $this->lastModified = new DateTime();
  48. $this->$attribute = $value;
  49. $this->notify(); // notify all observers have be registered to this instance
  50. }
  51. }
  52.  
  53.  
  54. class NoticeBoard implements Observer
  55. {
  56. private function drawSingleLine($char='-', $repeat=80)
  57. {
  58. while($repeat--) {
  59. echo $char;
  60. }
  61. echo '<br />';
  62. }
  63.  
  64. public function display($notice)
  65. {
  66. $this->drawSingleLine();
  67. echo "Title: {$notice->title}" . '<br />';
  68. echo "Content: {$notice->content}" . '<br />';
  69. echo "Last Modified At: {$notice->lastModified->format('Y-m-d H:i:s')}" . '<br />';
  70. $this->drawSingleLine();
  71. }
  72.  
  73. public function update($subject)
  74. {
  75. $this->drawSingleLine('=');
  76. echo '<strong>' . '[A notice has been updated]' . '</strong>';
  77. echo "The notice '{$subject->title}' has been modified." . '<br />';
  78. echo "New Content: {$subject->content}" . '<br />';
  79. echo "Last Modified At: {$subject->lastModified->format('Y-m-d H:i:s')}" . '<br />';
  80. $this->drawSingleLine('=');
  81. }
  82. }
  83.  
  84.  
  85. /**
  86. * Let the notice be displayed again while it being modified.
  87. *
  88. * @author TonySeek
  89. *
  90. */
  91. class NoticeDisplayAgainWhileModifying implements Observer
  92. {
  93. public function __construct($board)
  94. {
  95. $this->board = $board;
  96. }
  97.  
  98. public function update($subject)
  99. {
  100. $this->board->display($subject);
  101. }
  102. }
  103.  
  104.  
  105. $notice = new Notice('Be careful', 'This is a dangerous behavior.');
  106.  
  107. $board = new NoticeBoard();
  108. $displayAgain = new NoticeDisplayAgainWhileModifying($board);
  109.  
  110. $notice->attachObserver($board);
  111. $notice->attachObserver($displayAgain); // try to remove this line
  112.  
  113. // the first display
  114. $board->display($notice);
  115.  
  116. // modified the content
  117. echo "<br />Now, we try to modify the notice's content." . '<br />' . '<br />';
  118. $notice->content = 'The behavior is safty but you should be careful still.';
Add Comment
Please, Sign In to add comment