DotNetAge - Mvc & jQuery CMS
Hide sidebar

Bridge


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


Bridge

Participants


The classes and/or objects participating in this pattern are:
  • Abstraction
defines the abstraction's interface.

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
/// <summary>
/// Construct a new Abstraction object with
/// the given bridge
/// </summary>
/// <param name="bridge">The given bridge</param>
public Abstraction(Bridge bridge)
{
_bridge = bridge;
}
#endregion

#region Methods

/// <summary>
/// The method demonstrate the call for
/// the bridge object by its abstraction
/// </summary>
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


/// <summary>
/// Perform implementation A operation
/// </summary>
public void OperationImplementation()
{
Console.Write("BridgeImplementationA");
}

#endregion
}

public class BridgeImplementationB : Bridge
{
#region Bridge Members

/// <summary>
/// Perform implementation B operation
/// </summary>
public void OperationImplementation()
{
Console.Write("BridgeImplementationB");
}

#endregion
}

#endregion

 


    Average:
  • Reads
    (1206)
  • Permalink
Share to: Add to del.icio.us Digg! Share on Google Buzz Share on Facebook Reddit! Stumble it! Share on Twitter

Tag cloud

Anything in here will be replaced on browsers that support the canvas element