Php 用于简单DB连接的适配器模式的使用和继承
我在理解当前数据库连接类是如何设计为适配器时遇到了一个小问题。它提供了一个connect方法,该方法将调用父级的connect方法,即PEAR的MDB2Php 用于简单DB连接的适配器模式的使用和继承,php,database,design-patterns,Php,Database,Design Patterns,我在理解当前数据库连接类是如何设计为适配器时遇到了一个小问题。它提供了一个connect方法,该方法将调用父级的connect方法,即PEAR的MDB2 require_once(MDB2....) class Connection { //new/overloading methods that call parents'methods } 对于什么是适配器模式的基本定义,我认为连接是一个,但这让我想知道继承和使用中的适配器之间可能存在的差异 感谢您的解释 更新 我不明白的是,
require_once(MDB2....)
class Connection
{
//new/overloading methods that call parents'methods
}
对于什么是适配器模式的基本定义,我认为连接是一个,但这让我想知道继承和使用中的适配器之间可能存在的差异
感谢您的解释
更新
我不明白的是,如果我重新设计这个类
class Connection extends MDB2 //for example
{
// my new methods
// along with other overloading methods
}
我的连接仍被视为适配器吗
我不明白的是如果我重新设计这个类。。。我的连接仍被视为适配器吗
据我所知,适配器用于允许通常不设计为协同工作的对象仍然能够无缝操作。例如:
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);
我认为这不是适配器模式的一个好例子。从它自己的定义来看:适配器模式是一种将类的一个接口转换为兼容接口的设计模式。