[英]Adapter pattern in use and inheritance for a simple DB connection
我在嘗試了解當前的數據庫連接類是如何設計為適配器時遇到了一個小問題。 它提供了一個connect方法,該方法將調用父級的connect方法(即PEAR的MDB2)
require_once(MDB2....)
class Connection
{
//new/overloading methods that call parents'methods
}
對於什么是適配器模式的基本定義,我認為Connection是一個,但是這使我懷疑繼承和使用的適配器之間可能存在差異嗎?
任何解釋表示贊賞,謝謝。
更新資料
我不知道的是,如果我將課程重新設計為
class Connection extends MDB2 //for example
{
// my new methods
// along with other overloading methods
}
我的Connection是否仍被視為適配器?
我無法確定的是,如果我將類重新設計為
...
我的Connection是否仍被視為適配器?
據我了解,適配器用於允許通常不設計為可一起使用的對象仍然具有無縫運行的能力。 例如:
class TwoProngOutlet
{
public function connect($cord)
{
// Plug $cord into two prong outlet
}
}
class ThreeProngOutlet
{
public function connect($cord)
{
// Plug $cord into three prong outlet
}
}
class OutletAdapter
{
public function getOutlet($cord)
{
if($cord->prongs == 2)
{
return new TwoProngOutlet($cord);
}
else if($cord->prongs == 3)
{
return new ThreeProngOutlet($cord);
}
}
}
$adapter = new OutletAdapter;
$cord = new TwoProngCord;
// Adapt outlet to meet cord type
$outlet = $adapter->getOutlet($cord);
// Plug it in without having to know what type of outlet we are using
$outlet->connect($cord);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.