ariestamirra

temp method

Aug 2nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. <?php
  2. abstract class TemplateAbstract
  3. {
  4.     //the template method
  5.     // sets up a general algorithm for the whole class
  6.     public final function showBookTitleInfo($book_in)
  7.     {
  8.         $title           = $book_in->getTitle();
  9.         $author          = $book_in->getAuthor();
  10.         $processedTitle  = $this->processTitle($title);
  11.         $processedAuthor = $this->processAuthor($author);
  12.         if (NULL == $processedAuthor) {
  13.             $processed_info = $processedTitle;
  14.         } else {
  15.             $processed_info = $processedTitle . ' by ' . $processedAuthor;
  16.         }
  17.         return $processed_info;
  18.     }
  19.     //the primitive operation
  20.     // this function must be overridded
  21.     abstract function processTitle($title);
  22.     //the hook operation
  23.     // this function may be overridden,
  24.     // but does nothing if it is not
  25.     function processAuthor($author)
  26.     {
  27.         return NULL;
  28.     }
  29. }
  30. class TemplateExclaim extends TemplateAbstract
  31. {
  32.     function processTitle($title)
  33.     {
  34.         return Str_replace(' ', '!!!', $title);
  35.     }
  36.     function processAuthor($author)
  37.     {
  38.         return Str_replace(' ', '!!!', $author);
  39.     }
  40. }
  41. class TemplateStars extends TemplateAbstract
  42. {
  43.     function processTitle($title)
  44.     {
  45.         return Str_replace(' ', '*', $title);
  46.     }
  47. }
  48. class Book
  49. {
  50.     private $author;
  51.     private $title;
  52.     function __construct($title_in, $author_in)
  53.     {
  54.         $this->author = $author_in;
  55.         $this->title  = $title_in;
  56.     }
  57.     function getAuthor()
  58.     {
  59.         return $this->author;
  60.     }
  61.     function getTitle()
  62.     {
  63.         return $this->title;
  64.     }
  65.     function getAuthorAndTitle()
  66.     {
  67.         return $this->getTitle() . ' by ' . $this->getAuthor();
  68.     }
  69. }
  70. writeln('BEGIN TESTING TEMPLATE PATTERN');
  71. writeln('');
  72. $book            = new Book('PHP for Cats', 'Larry Truett');
  73. $exclaimTemplate = new TemplateExclaim();
  74. $starsTemplate   = new TemplateStars();
  75. writeln('test 1 - show exclaim template');
  76. writeln($exclaimTemplate->showBookTitleInfo($book));
  77. writeln('');
  78. writeln('test 2 - show stars template');
  79. writeln($starsTemplate->showBookTitleInfo($book));
  80. writeln('');
  81. writeln('END TESTING TEMPLATE PATTERN');
  82. function writeln($line_in)
  83. {
  84.     echo $line_in . "<br/>";
  85. }
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment