Bridge Design Pattern in C#


Bridge

Download full code from here.

Introduction

GOF definition: “Decouple an abstraction from its implementation so that the two can vary independently.” Bridge uses encaptulation, aggregation and use (probably) inheritence to separate responsibilities into different classes. This pattern is useful when both the abstraction as well as what it does often vary.

Motivation

Consider a situation that we have a different type of switch mechanism which has different implementation of ‘On’ and ‘Off’. For instance, Computer has different type of powering on/off mechanism than that of lamp. In this case we can use the implementation in a single class using if…else but what if we need to add those mechanism for water pump, mobile, vehicles. Then our code will be ugly if we continue to do the work using if…else. The Bridge design pattern proposes refactoring these issues by using two orthogonal hierarchies – one for independent abstractions, and the other for dependent implementations.

Architecture

This class structure shows that the Abstraction is completely separated from the implementation which can vary independently. Abstraction contains the instance of the implementor. In fact Abstraction behaves as wrapper of implementor but can have its own set of logic. Here I have shown only one refined abstraction but it can be many also according to the need of the logic.

Code

Complete code can be downloaded from here.

Here, I will give you the very simple example of the Bridge pattern which uses the one RefinedAbstraction and two different concrete implementations which defined the On/Off mechanism for Computer and Electric Lamp.

AbstractSwitch


abstract class AbstractSwitch

{

public AbstractSwitch(bool initialSwitchState)

{

_switchState = initialSwitchState;

}

public virtual bool On()

{

return _switchImplementation.On();

}

public virtual bool Off()

{

return _switchImplementation.Off();

}

public virtual void ShowInformation()

{

_switchImplementation.ShowInformation();

}

public SwitchImplementation SwitchImplementation

{

get { return _switchImplementation; }

set

{

_switchImplementation = value;

if (_switchState)

_switchImplementation.On();

}

}

private bool _switchState;

private SwitchImplementation _switchImplementation;

}

Notice the SwitchImplementation instance and it’s uses in methods in AbstactSwitch class.

Abstract Implementation


abstract class SwitchImplementation

{

public abstract bool On();

public abstract bool Off();

public abstract void ShowInformation();

}

Our every concrete implementation will extend the SwitchImplementation instance.

Conclusion

Unlike Adapter which makes things after designed Bridge makes things work before they are. Note that Bridge is designed up-front to let the abstraction and the implementation vary independently.

Advertisements

2 thoughts on “Bridge Design Pattern in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s