【发布时间】:2014-12-23 05:13:56
【问题描述】:
我对适配器模式的概念有点困惑。我发现适配器类与我通常编写的扩展类非常相似。那么,它们之间究竟有什么区别?
例如(来自 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();
}
}
第二种解决方案似乎要简单得多。那么它(以及一般的其他继承类)是否被视为适配器类?
【问题讨论】:
标签: php inheritance design-patterns adapter extends