04 Dec 2023
Advanced
The Bridge design pattern is a structural pattern that separates the abstraction (abstraction layer) from its implementation (implementation layer) so that both can vary independently. In C#, the Bridge pattern typically involves the following main components:
-
Abstraction:
- Declares an interface or abstract class that defines the abstraction's operations.
- Maintains a reference to an object of type
Implementor.
public abstract class Abstraction { protected IImplementor implementor; public Abstraction(IImplementor implementor) { this.implementor = implementor; } public abstract void Operation(); } -
Refined Abstraction:
- Extends the abstraction defined by
Abstraction. - Can add additional functionality to the abstraction.
public class RefinedAbstraction : Abstraction { public RefinedAbstraction(IImplementor implementor) : base(implementor) { } public override void Operation() { // Additional logic, if needed Console.WriteLine("RefinedAbstraction Operation"); implementor.Operation(); } } - Extends the abstraction defined by
-
Implementor:
- Declares an interface or abstract class that defines the implementation's operations.
public interface IImplementor { void Operation(); } -
Concrete Implementor:
- Implements the
IImplementorinterface. - Provides specific implementations for the operations defined in the
IImplementorinterface.
public class ConcreteImplementorA : IImplementor { public void Operation() { Console.WriteLine("ConcreteImplementorA Operation"); } }public class ConcreteImplementorB : IImplementor { public void Operation() { Console.WriteLine("ConcreteImplementorB Operation"); } } - Implements the
-
Client:
- Uses the abstraction to interact with the implementation.
class Client { static void Main() { IImplementor implementorA = new ConcreteImplementorA(); IImplementor implementorB = new ConcreteImplementorB(); Abstraction abstractionA = new RefinedAbstraction(implementorA); abstractionA.Operation(); Abstraction abstractionB = new RefinedAbstraction(implementorB); abstractionB.Operation(); } }
In this example:
Abstractiondeclares the interface for the "abstraction" part and maintains a reference to an object of typeIImplementor.RefinedAbstractionis a concrete class that extendsAbstractionand can add additional functionality.IImplementoris the interface for the "implementor" part.ConcreteImplementorAandConcreteImplementorBare concrete classes that implement theIImplementorinterface.- The
Clientinteracts with the abstraction and can use different implementations of the implementor.
The Bridge pattern allows the abstraction and implementation to evolve independently, making it easier to extend and maintain the code. It is especially useful when you want to avoid a permanent binding between an abstraction and its implementation and when changes in the implementation should not affect clients.