Adapter パターン

出典: フリー百科事典『ウィキペディア(Wikipedia)』

Adapter パターン(アダプター・パターン)とは、GoF によって定義されたデザインパターンの1つである。Adapter パターンを用いると、既存のクラスに対して修正を加えることなく、インタフェースを変更することができる。Adapter パターンを実現するための手法として継承を利用した手法と委譲を利用した手法が存在する。それぞれについて以下の節で説明する。

継承を利用したAdapter[編集]

継承を利用したAdapterは、利用したいクラスのサブクラスを作成し、そのサブクラスに対して必要なインタフェースを実装することで実現される。

サンプルプログラム[編集]

下記の例において、Productクラスは既存のクラスであり修正できないものとする。 ここで、Productクラスを利用したい開発者がいて、 その開発者はgetPriceというメソッドでProductの値段を取得したいとする。 この場合、ProductAdapterというAdapterを作成することで、既存クラス(Product)クラスを修正することなく、 異なるインタフェースを持たせることができる。 このように、既存クラスを修正することなく、異なるインタフェースを持たせるということが、Adapter パターンの役割である。

interface ProductPrice{
  public int getPrice();
}

class Product{
  private int cost;
  public int getCost(){
    return cost;
  }
}

class ProductAdapter extends Product implements ProductPrice{
  public int getPrice(){
    return this.getCost();
  }
}

クラス図[編集]

継承を利用したAdapterのクラス図は以下のようになる。

Adapter は Adaptee を継承し、同時に Target を実装する。実装したメソッド Adapter#requiredMethod() 内で Adaptee#oldMethod() を実行する
Adapter は Adaptee を継承し、同時に Target を実装する。実装したメソッド Adapter#requiredMethod() 内で Adaptee#oldMethod() を実行する

参考までに、上のサンプルコードとこのクラス図との対応を示す。

Target
ProductPrice
Target#requiredMethod
ProductPrice#getPrice()
Adapter
ProductAdapter
Adapter#requiredMethod
ProductAdapter#getPrice()
Adaptee
Product
Adaptee#oldMethod
Product#getCost()

委譲を利用したAdapter[編集]

委譲を利用したAdapterは、利用したいクラスのインスタンスを生成し、そのインスタンスを他クラスから利用することで実現される。

サンプルプログラム[編集]

interface ProductPrice{
  public int getPrice();
}

class Product{
  private int cost;
  public int getCost(){
    return cost;
  }
}

class ProductAdapter implements ProductPrice{
  private Product product = new Product();
  public int getPrice(){
    return product.getCost();
  }
}

クラス図[編集]

委譲を利用したAdapterのクラス図は以下のようになる。

Adapter は Adaptee をメンバに持ち、同時に Target を実装する。実装したメソッド Adapter#requiredMethod() 内で、メンバの Adaptee#oldMethod() を実行する
Adapter は Adaptee をメンバに持ち、同時に Target を実装する。実装したメソッド Adapter#requiredMethod() 内で、メンバの Adaptee#oldMethod() を実行する

※上図において、extendsはimplementsでも良い。

こちらのほうも、参考までにサンプルコードの対応を示す。

Target
ProductPrice
Target#requiredMethod()
ProductPrice#getPrice()
Adapter
ProductAdapter
Adapter#requiredMethod()
ProductAdapter#getPrice()
Adaptee
Product
Adaptee#oldMethod()
Product#getCost()