lundi 22 décembre 2014

PHP design patterns - inheritance vs adapter pattern?

I am a bit confused with the concept of adapter pattern. I find that adapter classes are very similar to extended classes that I would write usually. So, what is the differences between them actually?


For instance (an example from this link),


SimpleBook.php,



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;}


}


BookAdapter.php



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();
}


}


BookExtension.php,



include_once('SimpleBook.php');

class BookExtension extends SimpleBook{

function getAuthorAndTitle() {
return $this->getTitle() . ' by ' . $this->getAuthor();
}


}


The second solution seems a lot simpler. So is it (and other inheritance classes in general) considered as adapter class then?


Aucun commentaire:

Enregistrer un commentaire