Advertisement
ariestamirra

strategy

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