Friday, October 05, 2007

* Patterns : Bridge Structural Pattern

Bridge Structural Pattern decouples an abstraction from its implementation so that the two can vary independently. Multiple variants of abstraction can be created provided they implement the common interface. Similarly, multiple variants of implementations can be created provided they implement the common interface. Given the option of multiple abstraction variants and multiple implementation variants, clients have the flexibility of using any abstraction class with any implementation.

Consider the class diagram below :

 

Bridge

There is one variant of abstraction called AbstractionX and two variants of implementation ImplementationA and ImplementationB.

The client code looks like following :

public class Client
{
    AbstractionBase absBase;
    public void Run()
    {
        ImplementationA implA = new ImplementationA();
        ImplementationB implB = new ImplementationB();

        absBase = new AbstractionBase(implA);
        absBase.PrintString("Hello\n\r");
        absBase = new AbstractionBase(implB);
        absBase.PrintString("Hello\n\r");

        AbstractionX absX = new AbstractionX(implB);
        absX.PrintString("Hello\n\r");
    }
}

Client code shows that same abstraction can be used with multiple implementations and similarly same implementation can be used with multiple abstractions.

Download complete source code : Bridge Structural Pattern source code

No comments:

Post a Comment