0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GoF(Gang of Four)デザインパターン:抽象ファクトリ(Abstract Factory)

Posted at

GoF(Gang of Four)デザインパターンは、ソフトウェア設計における一連の再利用可能なソリューションです。GoFデザインパターンは、エリック・ガンマ、リチャード・ヘルム、ラルフ・ジョンソン、ジョン・ブリシディスによって定義され、彼らの著書『デザインパターン要素再利用のためのソフトウェアのオブジェクト指向分析と設計』で紹介されました。以下に、GoFデザインパターンの23種類を示します。

  1. デザインパターン: クリエーションパターン

    • 抽象ファクトリ(Abstract Factory)
    • ビルダ(Builder)
    • ファクトリメソッド(Factory Method)
    • プロトタイプ(Prototype)
    • シングルトン(Singleton)
  2. デザインパターン: 構造パターン

    • アダプタ(Adapter)
    • ブリッジ(Bridge)
    • コンポジット(Composite)
    • デコレータ(Decorator)
    • ファサード(Facade)
    • フライウェイト(Flyweight)
    • プロキシ(Proxy)
  3. デザインパターン: 振る舞いパターン

    • チェインオブレスポンシビリティ(Chain of Responsibility)
    • コマンド(Command)
    • イテレータ(Iterator)
    • メディエータ(Mediator)
    • メメント(Memento)
    • オブザーバ(Observer)
    • ステート(State)
    • ストラテジ(Strategy)
    • テンプレートメソッド(Template Method)
    • ビジター(Visitor)

これらのパターンは、異なるソフトウェア設計の問題を解決するためのガイドラインとして使用することができます。各パターンは、特定の問題領域に対して再利用可能なソリューションを提供し、コードの柔軟性、再利用性、保守性を向上させるのに役立ちます。ただし、すべての場合にこれらのパターンが最適であるわけではありません。問題の特性に合わせて適切なパターンを選択する必要があります。

抽象ファクトリパターンの別の例:UIコンポーネント

抽象ファクトリパターンを使って、プラットフォームに応じたスクロールバーを生成するシステムを実装する例を見てみましょう。

ステップ 1: 抽象製品のインターフェースを定義

まず、スクロールバーの基本インターフェースを定義します。

public interface ScrollBar {
    void scroll();
}

ステップ 2: 具象製品を作成

次に、異なるスタイルのスクロールバーを具象クラスとして実装します。

public class WindowsScrollBar implements ScrollBar {
    public void scroll() {
        System.out.println("Windowsスタイルのスクロールバーを動かす");
    }
}

public class MacOSScrollBar implements ScrollBar {
    public void scroll() {
        System.out.println("MacOSスタイルのスクロールバーを動かす");
    }
}

ステップ 3: 抽象ファクトリのインターフェースを定義

スクロールバーを生成するファクトリのインターフェースを定義します。

public interface GUIFactory {
    ScrollBar createScrollBar();
}

ステップ 4: 具象ファクトリを作成

プラットフォームに応じた具体的なファクトリクラスを作成します。

public class WindowsFactory implements GUIFactory {
    public ScrollBar createScrollBar() {
        return new WindowsScrollBar();
    }
}

public class MacOSFactory implements GUIFactory {
    public ScrollBar createScrollBar() {
        return new MacOSScrollBar();
    }
}

ステップ 5: クライアントコードでファクトリを使用

クライアントコードでファクトリを利用して、プラットフォームに適したスクロールバーを生成します。

public class Application {
    private ScrollBar scrollBar;

    public Application(GUIFactory factory) {
        scrollBar = factory.createScrollBar();
    }

    public void run() {
        scrollBar.scroll();
    }
}

// 実行例
public class Demo {
    public static void main(String[] args) {
        Application app = new Application(new WindowsFactory());
        app.run();
    }
}

この例ではApplicationクラスは、具体的なScrollBarの実装について何も知りません。WindowsFactoryまたはMacOSFactoryのどちらかがApplicationに渡され、適切なScrollBarインスタンスが生成されます。

まとめ

この例では、抽象ファクトリパターンを使用して異なるプラットフォーム向けのUIコンポーネントを生成する方法を示しました。クライアントコードは抽象ファクトリに依存しており、具体的なファクトリの実装には依存していません。これにより、クライアントコードの変更を行うことなく、新しいプラットフォーム向けのコンポーネントを容易に追加できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?