Design Patterns - Bridge Mode (Bridge Pattern)
Definition
The Bridge pattern is a design pattern that decouples an abstraction from its implementation, allowing them to vary independently. It is used when an abstraction may have multiple implementations, usually coordinated by inheritance.
C# Example
public abstract class Implementor
{
public abstract void Operation();
}
public class ImplementorA : Implementor
{
public override void Operation()
{
Console.WriteLine("I need to deal with the problem 10s");
}
}
public class ImplementorB : Implementor
{
public override void Operation()
{
Console.WriteLine("I need to deal with the problem 1s");
}
}
public abstract class Abstraction
{
public Implementor _implementor { get; set; }
public virtual void Operation()
{
_implementor.Operation();
}
}
public class AbstractionRefined : Abstraction
{
public override void Operation()
{
base.Operation();
}
}
public class Program
{
static void Main(string[] args)
{
Abstraction abstraction = new AbstractionRefined();
abstraction._implementor = new ImplementorA();
abstraction.Operation();
abstraction._implementor = new ImplementorB();
abstraction.Operation();
Console.ReadLine();
}
}
Bridge Mode Participants
- Abstraction: The definition of the abstract class interface, which maintains a pointer to an object of type
Implementor. - RefinedAbstraction: An expansion of the interface defined by
Abstraction. - Implementor: The definition of the class that implements the interface, which does not have to exactly match the
Abstractioninterface. In fact, the two interfaces can be completely different. - ConcreteImplementorA and ConcreteImplementorB: Implementors that implement an interface and define its implementation.
Bridge Mode Characteristics
- Separation of Interface and Implementation: The bridge mode separates the interface from its implementation, achieving a constant not bound on an interface.
- Dynamic Implementation: An abstract class may be configured at run time, and an object can even change its implementation at run time.
- Decoupling: The bridge mode decouples the abstraction from its implementation, reducing the dependence on the separation of
Implementorand achieving some compile-time. - Scalability: Independently of
AbstractionandImplementorhierarchies expansion. - Transparency: The implementation details of the client are hidden.
When to Use the Bridge Mode
- We do not want to have a fixed binding relationship between the abstract and implementation section.
- The abstract class and its implementation should be able to be expanded by a method of generating a subclass.
- The bridge mode may be combined and implemented in different portions of the abstract interface, and extend them separately.
- To achieve some modifications to respond to customers without affecting the abstract, that is, customers do not have to recompile the code.
- Customers want to completely hide the abstract implementation part.
- We want to achieve shared between multiple objects, but do not require customers to know this.
Benefits of the Bridge Mode
- Improved Scalability: Independently of
AbstractionandImplementorhierarchies expansion. - Improved Transparency: The implementation details of the client are hidden.
- Improved Maintainability: The bridge mode helps reduce the dependence on the separation of
Implementorand achieving some compile-time. - Improved Flexibility: The bridge mode decouples the abstraction from its implementation, allowing them to vary independently.