• English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
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





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
///
/// 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

 


    Average:
  • Reads
    (751)
  • Permalink
Share to:

Tag cloud

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