Decoratorパターンとは? わかりやすく解説

Weblio 辞書 > 辞書・百科事典 > 百科事典 > Decoratorパターンの意味・解説 

Decorator パターン

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2020/10/02 10:14 UTC 版)

ナビゲーションに移動 検索に移動

Decorator パターン(デコレータ・パターン)とは、GoF(Gang of Four; 4人のギャングたち)によって定義されたデザインパターンの1つである。 このパターンは、既存のオブジェクトに新しい機能や振る舞いを動的に追加することを可能にする。

クラス図

Decorator パターンのクラス図を以下に挙げる。

概要

Decorator パターンの方針は、既存のオブジェクトを新しい Decorator オブジェクトでラップすることである。 その方法として、Decorator のコンストラクタの引数でラップ対象の Component オブジェクトを読み込み、コンストラクタの内部でそのオブジェクトをメンバに設定することが一般的である。

Decorator パターンは、既存のクラスを拡張する際にクラスの継承の代替手段として用いられる。継承がコンパイル時に機能を拡張するのに対し、Decorator パターンはプログラムの実行時に機能追加をする点が異なる。


利用例

Java による利用例を以下に挙げる。

/** 価格をあらわすインタフェース */
interface Price{
    int getValue();
}

/** 原価を表すクラス */
class PrimePrice implements Price{
    private int value;
    PrimePrice(int value){
        this.value = value;
    }
    public int getValue(){
        return this.value;
    }
}

/** マージンを介する価格 */
abstract class MarginPrice implements Price{
    protected Price originalPrice;
    MarginPrice(Price price){
        this.originalPrice = price;
    }
}

/** 設定された利益を仕入れ価格に上乗せする Price */
class WholesalePrice extends MarginPrice{
    private int advantage;
    WholesalePrice(Price price, int advantage){
        super(price);
        this.advantage = advantage;
    }
    public int getValue(){
        return this.originalPrice.getValue() + advantage;
    }
}

/** 仕入れ価格の 2 倍の値段を提示する Price */
class DoublePrice extends MarginPrice{
    DoublePrice(Price price){
        super(price);
    }
    public int getValue(){
        return this.originalPrice.getValue() * 2;
    }
}

public class DecoratorTest{
    public static void main(String[] argv){
        System.out.println(
            new WholesalePrice(
                new DoublePrice(
                    new WholesalePrice(
                        new DoublePrice(
                            new PrimePrice(120)
                            )
                        ,80
                        )
                    )
                ,200
                )
            .getValue()
            );
    }
}

クラス図との関連は以下の通りである。

Component
Price
ConcreteComponent
PrimePrice
Decorator
MarginPrice
ConcreteDecorator
WholesalePrice, DoublePrice

関連項目


「Decorator パターン」の例文・使い方・用例・文例

Weblio日本語例文用例辞書はプログラムで機械的に例文を生成しているため、不適切な項目が含まれていることもあります。ご了承くださいませ。


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

辞書ショートカット

すべての辞書の索引

「Decoratorパターン」の関連用語

Decoratorパターンのお隣キーワード
検索ランキング

   

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



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

   
ウィキペディアウィキペディア
All text is available under the terms of the GNU Free Documentation License.
この記事は、ウィキペディアのDecorator パターン (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。 Weblio辞書に掲載されているウィキペディアの記事も、全てGNU Free Documentation Licenseの元に提供されております。
Tanaka Corpusのコンテンツは、特に明示されている場合を除いて、次のライセンスに従います:
 Creative Commons Attribution (CC-BY) 2.0 France.
この対訳データはCreative Commons Attribution 3.0 Unportedでライセンスされています。
浜島書店 Catch a Wave
Copyright © 1995-2025 Hamajima Shoten, Publishers. All rights reserved.
株式会社ベネッセコーポレーション株式会社ベネッセコーポレーション
Copyright © Benesse Holdings, Inc. All rights reserved.
研究社研究社
Copyright (c) 1995-2025 Kenkyusha Co., Ltd. All rights reserved.
日本語WordNet日本語WordNet
日本語ワードネット1.1版 (C) 情報通信研究機構, 2009-2010 License All rights reserved.
WordNet 3.0 Copyright 2006 by Princeton University. All rights reserved. License
日外アソシエーツ株式会社日外アソシエーツ株式会社
Copyright (C) 1994- Nichigai Associates, Inc., All rights reserved.
「斎藤和英大辞典」斎藤秀三郎著、日外アソシエーツ辞書編集部編
EDRDGEDRDG
This page uses the JMdict dictionary files. These files are the property of the Electronic Dictionary Research and Development Group, and are used in conformance with the Group's licence.

©2025 GRAS Group, Inc.RSS