Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'observer.php';
- require_once 'subject.php';
- use Common\Observer\Observer;
- use Common\Observer\Subject;
- class Notice
- {
- use Subject;
- /**
- * The title
- * @var string
- */
- private $title = '';
- /**
- * The notice content
- * @var unknown_type
- */
- private $content = '';
- /**
- * The last modified time
- * @var unknown_type
- */
- private $lastModified = null;
- /**
- * The constructor
- */
- public function __construct($title, $content)
- {
- $this->title = $title;
- $this->content = $content;
- $this->lastModified = new DateTime();
- }
- public function __get($attribute)
- {
- return $this->$attribute;
- }
- public function __set($attribute, $value)
- {
- $this->lastModified = new DateTime();
- $this->$attribute = $value;
- $this->notify(); // notify all observers have be registered to this instance
- }
- }
- class NoticeBoard implements Observer
- {
- private function drawSingleLine($char='-', $repeat=80)
- {
- while($repeat--) {
- echo $char;
- }
- echo '<br />';
- }
- public function display($notice)
- {
- $this->drawSingleLine();
- echo "Title: {$notice->title}" . '<br />';
- echo "Content: {$notice->content}" . '<br />';
- echo "Last Modified At: {$notice->lastModified->format('Y-m-d H:i:s')}" . '<br />';
- $this->drawSingleLine();
- }
- public function update($subject)
- {
- $this->drawSingleLine('=');
- echo '<strong>' . '[A notice has been updated]' . '</strong>';
- echo "The notice '{$subject->title}' has been modified." . '<br />';
- echo "New Content: {$subject->content}" . '<br />';
- echo "Last Modified At: {$subject->lastModified->format('Y-m-d H:i:s')}" . '<br />';
- $this->drawSingleLine('=');
- }
- }
- /**
- * Let the notice be displayed again while it being modified.
- *
- * @author TonySeek
- *
- */
- class NoticeDisplayAgainWhileModifying implements Observer
- {
- public function __construct($board)
- {
- $this->board = $board;
- }
- public function update($subject)
- {
- $this->board->display($subject);
- }
- }
- $notice = new Notice('Be careful', 'This is a dangerous behavior.');
- $board = new NoticeBoard();
- $displayAgain = new NoticeDisplayAgainWhileModifying($board);
- $notice->attachObserver($board);
- $notice->attachObserver($displayAgain); // try to remove this line
- // the first display
- $board->display($notice);
- // modified the content
- echo "<br />Now, we try to modify the notice's content." . '<br />' . '<br />';
- $notice->content = 'The behavior is safty but you should be careful still.';
Add Comment
Please, Sign In to add comment