Adapters are used to enable objects with different interfaces to communicate with each other.Convert the interface of a class into another interface clients expect. It is useful when we upgrade system
<?php
//SimpleBook.php copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
class SimpleBook
{
private $author;
private $title;
function __construct($author_in, $title_in) {
$this->author = $author_in;
$this->title = $title_in;
}
function getAuthor() {return $this->author;}
function getTitle() {return $this->title;}
}
?>
<?php
//BookAdapter.php copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('SimpleBook.php');
class BookAdapter
{
private $book;
function __construct(SimpleBook $book_in) {
$this->book = $book_in;
}
function getAuthorAndTitle() {
return $this->book->getTitle() . ' by ' . $this->book->getAuthor();
}
}
?>
<?php
//testAdapter.php copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved
include_once('SimpleBook.php');
include_once('BookAdapter.php');
define('BR', '<'.'BR'.'>');
echo 'BEGIN TESTING ADAPTER PATTERN'.BR;
echo BR;
$book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides","Design Patterns");
$bookAdapter = new BookAdapter($book);
echo 'Author and Title: '.$bookAdapter->getAuthorAndTitle();
echo BR.BR;
echo 'END TESTING ADAPTER PATTERN'.BR;
?>
Source: From lecture notes of Design and Development Open Multi-tier Application
No comments:
Post a Comment