Adapter patternとは? わかりやすく解説

Weblio 辞書 > 辞書・百科事典 > 百科事典 > Adapter patternの意味・解説 

Adapter パターン

(Adapter pattern から転送)

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2024/01/13 01:36 UTC 版)

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() を実行する

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

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() を実行する

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

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

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



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Adapter pattern」の関連用語

Adapter patternのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Adapter patternのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
ウィキペディアウィキペディア
All text is available under the terms of the GNU Free Documentation License.
この記事は、ウィキペディアのAdapter パターン (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。 Weblio辞書に掲載されているウィキペディアの記事も、全てGNU Free Documentation Licenseの元に提供されております。

©2025 GRAS Group, Inc.RSS