Programming - Bridge Structural Design Pattern


Decouple an abstraction from its implementation so that the two can vary independently.


The classes and/or objects participating in this pattern are:


  • Abstractionand
    and and
    • defines the abstraction's interface.
    • maintains a reference to an object of type Implementor.
  • RefinedAbstractionand
    and
    • extends the interface defined by Abstraction.
  • Implementorand
    and and
    • 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.
// Bridge pattern -- Structural example

using
System;

namespace
DoFactory.GangOfFour.Bridge.Structural
{

// MainApp test application

class MainApp
{
static void Main()
{
Abstraction ab = new RefinedAbstraction();

// Set implementation and call
ab.Implementor = new ConcreteImplementorA();
ab.Operation();

// Change implemention and call
ab.Implementor = new ConcreteImplementorB();
ab.Operation();

// Wait for user
Console.Read();
}
}

// "Abstraction"

class Abstraction
{
protected Implementor implementor;

// Property
public Implementor Implementor
{
set{ implementor = value; }
}

public virtual void Operation()
{
implementor.Operation();
}
}

// "Implementor"

abstract class Implementor
{
public abstract void Operation();
}

// "RefinedAbstraction"

class RefinedAbstraction : Abstraction
{
public override void Operation()
{
implementor.Operation();
}
}

// "ConcreteImplementorA"

class ConcreteImplementorA : Implementor
{
public override void Operation()
{
Console.WriteLine("ConcreteImplementorA Operation");
}
}

// "ConcreteImplementorB"

class ConcreteImplementorB : Implementor
{
public override void Operation()
{
Console.WriteLine("ConcreteImplementorB Operation");
}
}
}
and
Output expected:
ConcreteImplementorA Operation
ConcreteImplementorB Operation


Home | Privacy Policy | Site Map | Links | Company | Contact Us | Arena           

We provide cutting edge software development, consultancy and web design services            

There are currently 640 user(s) with 40671 hits.