Bridgeパターンとは | GoFデザインパターン

記事内に広告が含まれています。

Bridgeパターンは、GoF(Gang of Four)が定義した23のデザインパターンの一つです。

このパターンは、機能の抽象部分と実装部分を分離し、それぞれが独立して変更可能になるように設計されています。

このページでは、Bridgeパターンの概要、使い方、そしてJava、C++、C#、VB.NETのコードサンプルを使って実装方法を解説します。

Bridgeパターンとは

Bridgeパターンは、クラスの階層を異なる次元に分離することで、システムの柔軟性と再利用性を高めるために使用されます。

特に、機能の抽象部分(Abstraction)と実装部分(Implementation)を分離することで、異なる実装に簡単に対応できるようになります。

Bridgeパターンの目的

Bridgeパターンの主な目的は、システムの抽象部分と実装部分を別々に管理し、それぞれ独立して変更可能にすることです。

これにより、新しい実装が追加された場合でも、既存の抽象部分を変更することなく対応できます。

Bridgeパターンの使い方

Bridgeパターンを使用することで、特定の機能を抽象的に定義し、異なる実装を持つ複数のオブジェクト間で切り替え可能にすることができます。

これにより、クラスの数を増やさずに、異なる実装の追加や変更が可能になります。

Bridgeパターンの構造

Bridgeパターンは、以下のクラスやインターフェースで構成されます。

  • Abstraction: 機能の抽象部分を定義する役割。
  • Implementor: 実装のインターフェースを定義します。
  • RefinedAbstraction: Abstractionの拡張クラス。
  • ConcreteImplementor: Implementorインターフェースを実装する具体的なクラス。

Bridgeパターン実装サンプル

次に、Java、C++、C#、VB.NETでのBridgeパターンの実装例を示します。

JavaでのBridgeパターン実装


// Implementor interface
interface Implementor {
    void operationImpl();
}

// Concrete Implementor 1
class ConcreteImplementor1 implements Implementor {
    public void operationImpl() {
        System.out.println("ConcreteImplementor1 operation");
    }
}

// Concrete Implementor 2
class ConcreteImplementor2 implements Implementor {
    public void operationImpl() {
        System.out.println("ConcreteImplementor2 operation");
    }
}

// Abstraction class
abstract class Abstraction {
    protected Implementor implementor;
    
    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }
    
    abstract void operation();
}

// Refined Abstraction
class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }
    
    public void operation() {
        implementor.operationImpl();
    }
}

// Main method
public class BridgePatternDemo {
    public static void main(String[] args) {
        Implementor impl1 = new ConcreteImplementor1();
        Abstraction abstraction1 = new RefinedAbstraction(impl1);
        abstraction1.operation();
        
        Implementor impl2 = new ConcreteImplementor2();
        Abstraction abstraction2 = new RefinedAbstraction(impl2);
        abstraction2.operation();
    }
}

C++でのBridgeパターン実装


#include 
using namespace std;

// Implementor interface
class Implementor {
public:
    virtual void operationImpl() = 0;
};

// Concrete Implementor 1
class ConcreteImplementor1 : public Implementor {
public:
    void operationImpl() {
        cout << "ConcreteImplementor1 operation" << endl;
    }
};

// Concrete Implementor 2
class ConcreteImplementor2 : public Implementor {
public:
    void operationImpl() {
        cout << "ConcreteImplementor2 operation" << endl;
    }
};

// Abstraction class
class Abstraction {
protected:
    Implementor* implementor;
public:
    Abstraction(Implementor* imp) : implementor(imp) {}
    virtual void operation() = 0;
};

// Refined Abstraction
class RefinedAbstraction : public Abstraction {
public:
    RefinedAbstraction(Implementor* imp) : Abstraction(imp) {}
    void operation() {
        implementor->operationImpl();
    }
};

// Main function
int main() {
    Implementor* impl1 = new ConcreteImplementor1();
    Abstraction* abstraction1 = new RefinedAbstraction(impl1);
    abstraction1->operation();
    
    Implementor* impl2 = new ConcreteImplementor2();
    Abstraction* abstraction2 = new RefinedAbstraction(impl2);
    abstraction2->operation();
    
    delete impl1;
    delete impl2;
    delete abstraction1;
    delete abstraction2;
    
    return 0;
}

C#でのBridgeパターン実装


using System;

// Implementor interface
interface Implementor {
    void OperationImpl();
}

// Concrete Implementor 1
class ConcreteImplementor1 : Implementor {
    public void OperationImpl() {
        Console.WriteLine("ConcreteImplementor1 operation");
    }
}

// Concrete Implementor 2
class ConcreteImplementor2 : Implementor {
    public void OperationImpl() {
        Console.WriteLine("ConcreteImplementor2 operation");
    }
}

// Abstraction class
abstract class Abstraction {
    protected Implementor implementor;

    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }

    public abstract void Operation();
}

// Refined Abstraction
class RefinedAbstraction : Abstraction {
    public RefinedAbstraction(Implementor implementor) : base(implementor) { }

    public override void Operation() {
        implementor.OperationImpl();
    }
}

// Main class
class Program {
    static void Main(string[] args) {
        Implementor impl1 = new ConcreteImplementor1();
        Abstraction abstraction1 = new RefinedAbstraction(impl1);
        abstraction1.Operation();
        
        Implementor impl2 = new ConcreteImplementor2();
        Abstraction abstraction2 = new RefinedAbstraction(impl2);
        abstraction2.Operation();
    }
}

VB.NETでのBridgeパターン実装


Public Interface Implementor
    Sub OperationImpl()
End Interface

Public Class ConcreteImplementor1
    Implements Implementor
    Public Sub OperationImpl() Implements Implementor.OperationImpl
        Console.WriteLine("ConcreteImplementor1 operation")
    End Sub
End Class

Public Class ConcreteImplementor2
    Implements Implementor
    Public Sub OperationImpl() Implements Implementor.OperationImpl
        Console.WriteLine("ConcreteImplementor2 operation")
    End Sub
End Class

Public MustInherit Class Abstraction
    Protected implementor As Implementor

    Public Sub New(impl As Implementor)
        implementor = impl
    End Sub

    Public MustOverride Sub Operation()
End Class

Public Class RefinedAbstraction
    Inherits Abstraction

    Public Sub New(impl As Implementor)
        MyBase.New(impl)
    End Sub

    Public Overrides Sub Operation()
        implementor.OperationImpl()
    End Sub
End Class

Module Program
    Sub Main()
        Dim impl1 As Implementor = New ConcreteImplementor1()
        Dim abstraction1 As Abstraction = New RefinedAbstraction(impl1)
        abstraction1.Operation()

        Dim impl2 As Implementor = New ConcreteImplementor2()
        Dim abstraction2 As Abstraction = New RefinedAbstraction(impl2)
        abstraction2.Operation()
    End Sub
End Module

まとめ

Bridgeパターンは、システムの拡張性と柔軟性を高めるための重要なデザインパターンです。

抽象部分と実装部分を分離することで、異なる実装を簡単に追加・変更できるようになります。

Java、C++、C#、VB.NETなど、さまざまなプログラミング言語で実装できるため、さまざまなプロジェクトに適用可能です。

ぜひ自分のプロジェクトにも活用してみてください。

オブジェクト指向プログラミングにおけるGoFの23のデザインパターン
デザインパターンは、ソフトウェア設計の問題を効果的に解決するための再利用可能な解決策です。本記事では、GoF(Gang ...
タイトルとURLをコピーしました