fork download
  1. <?php
  2.  
  3. class SimpleBook {
  4. private $author;
  5. private $title;
  6. function __construct($author_in, $title_in) {
  7. $this->author = $author_in;
  8. $this->title = $title_in;
  9. }
  10. function getAuthor() {
  11. return $this->author;
  12. }
  13. function getTitle() {
  14. return $this->title;
  15. }
  16. }
  17.  
  18. class BookAdapter {
  19. private $book;
  20. function __construct(SimpleBook $book_in) {
  21. $this->book = $book_in;
  22. }
  23. function getAuthorAndTitle() {
  24. return $this->book->getTitle().' by '.$this->book->getAuthor();
  25. }
  26. }
  27.  
  28. // client
  29.  
  30. writeln('BEGIN TESTING ADAPTER PATTERN');
  31. writeln('');
  32.  
  33. $book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides", "Design Patterns");
  34. $bookAdapter = new BookAdapter($book);
  35. writeln('Author and Title: '.$bookAdapter->getAuthorAndTitle());
  36. writeln('');
  37.  
  38. writeln('END TESTING ADAPTER PATTERN');
  39.  
  40. function writeln($line_in) {
  41. echo $line_in."<br/>";
  42. }
  43.  
Success #stdin #stdout 0.02s 82880KB
stdin
Standard input is empty
stdout
BEGIN TESTING ADAPTER PATTERN<br/><br/>Author and Title: Design Patterns by Gamma, Helm, Johnson, and Vlissides<br/><br/>END TESTING ADAPTER PATTERN<br/>