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

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

Adapter パターン

(adapter pattern から転送)

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2026/02/17 20:22 UTC 版)

Adapter パターン(アダプター・パターン、英: Adapter pattern)は、ソフトウェア工学におけるデザインパターンの一つで、既存クラスを変更せずに、互換性のないインターフェースを接続可能にするため、あるインタフェースをクライアントが期待する別のインタフェースへ変換する設計手法である。実装形態には、継承を用いるクラスアダプタと、委譲オブジェクト合成)を用いるオブジェクトアダプタがある。

継承を利用した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()



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「adapter pattern」の関連用語

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

   

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



adapter patternのページの著作権

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

©2026 GRAS Group, Inc.RSS