2012-03-19 7 views
0

私の現在のデータベース接続クラスがどのようにアダプタとして設計されているかを理解するのに少し問題があります。これは、Adapterパターンが何であるかの基本的な定義にPEARのMDB2単純なDB接続のための使用と継承のアダプタパターン

require_once(MDB2....) 

class Connection 
{ 
    //new/overloading methods that call parents'methods 
} 

ある親のconnectメソッドを呼び出しますconnectメソッドを提供し、私は接続が1であることをそれを取るが、それは私が間の相違が可能に思ってしまいます継承とアダプターは使用中ですか?

何か説明をいただきありがとうございます。

更新

私は私が私の接続がまだアダプタと考えられている

class Connection extends MDB2 //for example 
{ 
    // my new methods 
    // along with other overloading methods 
} 

としてクラスを再設計されているかどうかを把握することはできませんか?

答えて

0

私は、...がまだアダプタと見なされているため、クラスを再設計してもらえませんか?

正常に動作するように設計されていないオブジェクトでも、シームレスに操作できるアダプタを使用するためにアダプタが使用されています。例:

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); 
+0

これはアダプタパターンの良い例ではないと思います。独自の定義から:「アダプター・パターンは、クラスの1つのインターフェースを互換インターフェースに変換する設計パターンです。 http://en.wikipedia.org/wiki/Adapter_pattern – XuDing

関連する問題