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 :



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 = new AbstractionBase(implB);

        AbstractionX absX = new AbstractionX(implB);

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