ariestamirra

bride

Aug 2nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class BridgeBook {    
  4.     private $bbAuthor;
  5.     private $bbTitle;
  6.     private $bbImp;
  7.     function __construct($author_in, $title_in, $choice_in) {
  8.       $this->bbAuthor = $author_in;
  9.       $this->bbTitle  = $title_in;
  10.       if ('STARS' == $choice_in) {
  11.         $this->bbImp = new BridgeBookStarsImp();
  12.       } else {
  13.         $this->bbImp = new BridgeBookCapsImp();
  14.       }
  15.     }    
  16.     function showAuthor() {
  17.       return $this->bbImp->showAuthor($this->bbAuthor);
  18.     }
  19.     function showTitle() {
  20.       return $this->bbImp->showTitle($this->bbTitle);
  21.     }
  22. }
  23.  
  24. class BridgeBookAuthorTitle extends BridgeBook {    
  25.     function showAuthorTitle() {
  26.       return $this->showAuthor() . "'s " . $this->showTitle();
  27.     }
  28. }  
  29.  
  30. class BridgeBookTitleAuthor extends BridgeBook {    
  31.     function showTitleAuthor() {
  32.       return $this->showTitle() . ' by ' . $this->showAuthor();
  33.     }
  34. }
  35.  
  36. abstract class BridgeBookImp {    
  37.     abstract function showAuthor($author);
  38.     abstract function showTitle($title);
  39. }
  40.  
  41. class BridgeBookCapsImp extends BridgeBookImp {    
  42.     function showAuthor($author_in) {
  43.       return strtoupper($author_in);
  44.     }
  45.     function showTitle($title_in) {
  46.       return strtoupper($title_in);
  47.     }
  48. }
  49.  
  50. class BridgeBookStarsImp extends BridgeBookImp {    
  51.     function showAuthor($author_in) {
  52.       return Str_replace(" ","*",$author_in);
  53.     }
  54.     function showTitle($title_in) {
  55.       return Str_replace(" ","*",$title_in);
  56.     }
  57. }
  58.  
  59.   writeln('BEGIN TESTING BRIDGE PATTERN');
  60.   writeln('');
  61.  
  62.   writeln('test 1 - author title with caps');
  63.   $book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','CAPS');
  64.   writeln($book->showAuthorTitle());
  65.   writeln('');
  66.  
  67.   writeln('test 2 - author title with stars');
  68.   $book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','STARS');
  69.   writeln($book->showAuthorTitle());
  70.   writeln('');
  71.  
  72.   writeln('test 3 - title author with caps');
  73.   $book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','CAPS');
  74.   writeln($book->showTitleAuthor());
  75.   writeln('');
  76.  
  77.   writeln('test 4 - title author with stars');
  78.   $book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','STARS');
  79.   writeln($book->showTitleAuthor());
  80.   writeln('');
  81.  
  82.   writeln('END TESTING BRIDGE PATTERN');
  83.  
  84.   function writeln($line_in) {
  85.     echo $line_in."<br/>";
  86.   }
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment