GoF:Abstract Factory

公開:2026.06.01(月) 00:06

Abstract Factory

Abstract Factory(アブストラクトファクトリー)は、関連するオブジェクト群(ファミリー)を具体的なクラスを指定せずに生成するためのデザインパターンです。Visual Basic(VB.NET)では、インターフェースやMustInherit(抽象クラス)を組み合わせて実装します。

構成要素

AbstractFactory(抽象工場)
製品群を生成するためのインターフェース

ConcreteFactory(具象工場)
AbstractFactoryを実装し、具体的な製品インスタンスを生成

AbstractProduct(抽象製品)
生成される製品のインターフェース

ConcreteProduct(具象製品)
AbstractProductの具体的な実装

Visual Basic(VB.NET)実装例

テーマ(Windows用/Mac用などのUI部品)ごとに一連のオブジェクトを作成する例です。
Module Module1

    ' 1. Abstract Products
    Public Interface IButton
        Sub Render()
    End Interface

    Public Interface ITextBox
        Sub Render()
    End Interface

    ' 2. Concrete Products (Windows)
    Public Class WindowsButton
        Implements IButton
        Public Sub Render() Implements IButton.Render
            Console.WriteLine("Windowsボタンを描画しました。")
        End Sub
    End Class

    Public Class WindowsTextBox
        Implements ITextBox
        Public Sub Render() Implements ITextBox.Render
            Console.WriteLine("Windowsテキストボックスを描画しました。")
        End Sub
    End Class

    ' 2. Concrete Products (Mac)
    Public Class MacButton
        Implements IButton
        Public Sub Render() Implements IButton.Render
            Console.WriteLine("Macボタンを描画しました。")
        End Sub
    End Class

    Public Class MacTextBox
        Implements ITextBox
        Public Sub Render() Implements ITextBox.Render
            Console.WriteLine("Macテキストボックスを描画しました。")
        End Sub
    End Class

    ' 3. Abstract Factory
    Public Interface IGUIFactory
        Function CreateButton() As IButton
            Function CreateTextBox() As ITextBox
    End Interface

    ' 4. Concrete Factories
    Public Class WindowsFactory
        Implements IGUIFactory
        Public Function CreateButton() As IButton Implements IGUIFactory.CreateButton
            Return New WindowsButton()
        End Function
        Public Function CreateTextBox() As ITextBox Implements IGUIFactory.CreateTextBox
            Return New WindowsTextBox()
        End Function
    End Class

    Public Class MacFactory
        Implements IGUIFactory
        Public Function CreateButton() As IButton Implements IGUIFactory.CreateButton
            Return New MacButton()
        End Function
        Public Function CreateTextBox() As ITextBox Implements IGUIFactory.CreateTextBox
            Return New MacTextBox()
        End Function
    End Class

    ' 5. Client
    Public Class Application
        Private _button As IButton
        Private _textBox As ITextBox

        Public Sub New(factory As IGUIFactory)
            _button = factory.CreateButton()
            _textBox = factory.CreateTextBox()
        End Sub

        Public Sub Paint()
            _button.Render()
            _textBox.Render()
        End Sub
    End Class

    ' 実行
    Public Sub Main()
        ' Windows環境向けのファクトリーを使用
        Dim factory As IGUIFactory = New WindowsFactory()
        Dim app As New Application(factory)
        app.Paint()

        ' Mac環境向けに切り替える場合も、クライアント側のコード変更は不要
        Dim factoryMac As IGUIFactory = New MacFactory()
        Dim appMac As New Application(factoryMac)
        appMac.Paint()
    End Sub

End Module


0 件のコメント:

その他の記事