ariestamirra

state

Aug 2nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1. <?php
  2. class BookContext
  3. {
  4.     private $book = NULL;
  5.     private $bookTitleState = NULL;
  6.     //bookList is not instantiated at construct time
  7.     public function __construct($book_in)
  8.     {
  9.         $this->book = $book_in;
  10.         $this->setTitleState(new BookTitleStateStars());
  11.     }
  12.     public function getBookTitle()
  13.     {
  14.         return $this->bookTitleState->showTitle($this);
  15.     }
  16.     public function getBook()
  17.     {
  18.         return $this->book;
  19.     }
  20.     public function setTitleState($titleState_in)
  21.     {
  22.         $this->bookTitleState = $titleState_in;
  23.     }
  24. }
  25. interface BookTitleStateInterface
  26. {
  27.     public function showTitle($context_in);
  28. }
  29. class BookTitleStateExclaim implements BookTitleStateInterface
  30. {
  31.     private $titleCount = 0;
  32.     public function showTitle($context_in)
  33.     {
  34.         $title = $context_in->getBook()->getTitle();
  35.         $this->titleCount++;
  36.         $context_in->setTitleState(new BookTitleStateStars());
  37.         return Str_replace(' ', '!', $title);
  38.     }
  39. }
  40. class BookTitleStateStars implements BookTitleStateInterface
  41. {
  42.     private $titleCount = 0;
  43.     public function showTitle($context_in)
  44.     {
  45.         $title = $context_in->getBook()->getTitle();
  46.         $this->titleCount++;
  47.         if (1 < $this->titleCount) {
  48.             $context_in->setTitleState(new BookTitleStateExclaim);
  49.         }
  50.         return Str_replace(' ', '*', $title);
  51.     }
  52. }
  53. class Book
  54. {
  55.     private $author;
  56.     private $title;
  57.     function __construct($title_in, $author_in)
  58.     {
  59.         $this->author = $author_in;
  60.         $this->title  = $title_in;
  61.     }
  62.     function getAuthor()
  63.     {
  64.         return $this->author;
  65.     }
  66.     function getTitle()
  67.     {
  68.         return $this->title;
  69.     }
  70.     function getAuthorAndTitle()
  71.     {
  72.         return $this->getTitle() . ' by ' . $this->getAuthor();
  73.     }
  74. }
  75. writeln('BEGIN TESTING STATE PATTERN');
  76. writeln('');
  77. $book = new Book('PHP for Cats', 'Larry Truett');
  78. ;
  79. $context = new bookContext($book);
  80. writeln('test 1 - show name');
  81. writeln($context->getBookTitle());
  82. writeln('');
  83. writeln('test 2 - show name');
  84. writeln($context->getBookTitle());
  85. writeln('');
  86. writeln('test 3 - show name');
  87. writeln($context->getBookTitle());
  88. writeln('');
  89. writeln('test 4 - show name');
  90. writeln($context->getBookTitle());
  91. writeln('');
  92. writeln('END TESTING STATE PATTERN');
  93. function writeln($line_in)
  94. {
  95.     echo $line_in . "<br/>";
  96. }
Advertisement
Add Comment
Please, Sign In to add comment