Definition
Decouple an abstraction from its implementation so that the two can vary independently.
Summary
The bridge pattern decouples an abstraction from its implementation so the two can vary independently. In other words we make a bridge between the abstraction and its implementation and therefore we won't have a
binding between the two. The pattern helps us whenever we need to select or switch the implementation at runtime.
UML class diagram
Participants
The classes and/or objects participating in this pattern are:
- Abstraction
maintains a reference to an object of type Implementor.
- RefinedAbstraction - extends the interface defined by Abstraction.
- Implementor - defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface; in fact the two interfaces can be quite different. Typically the Implementation interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.
- ConcreteImplementor - implements the Implementor interface and defines its concrete implementation.
Sample code in C#
#region The Abstraction
public class Abstraction
{
#region Members
private Bridge _bridge;
#endregion
#region Ctor
///
/// Construct a new Abstraction object with
/// the given bridge
///
/// The given bridge
public Abstraction(Bridge bridge)
{
_bridge = bridge;
}
#endregion
#region Methods
///
/// The method demonstrate the call for
/// the bridge object by its abstraction
///
public void Operation()
{
Console.Write("Using");
_bridge.OperationImplementation();
}
#endregion
}
#endregion
#region The Bridge And Its Implementations
public interface Bridge
{
void OperationImplementation();
}
public class BridgeImplementationA : Bridge
{
#region Bridge Members
///
/// Perform implementation A operation
///
public void OperationImplementation()
{
Console.Write("BridgeImplementationA");
}
#endregion
}
public class BridgeImplementationB : Bridge
{
#region Bridge Members
///
/// Perform implementation B operation
///
public void OperationImplementation()
{
Console.Write("BridgeImplementationB");
}
#endregion
}
#endregion
-
-
Reads(751)
-
Permalink