Quick and Short: Bridge Pattern

By , March 16, 2010

This article is written under ‘Quick and Short’ edition. In this article I will explain about Bridge Design Patterns.

Read more Quick and Short articles.

Read more about design patterns [Factory], [Abstract Factory], [Singleton], [Builder], [Prototype], [Adapter], [Bridge], [Composite], [Decorator], [Facade], [Proxy], [Flyweight], [Chain of Responsibility], [Command], [Iterator], [Mediator], [Memento], [Observer], [State], [Strategy], [Visitor], [Template Method], [Interpreter]

Bridge Design Patterns

Bridge pattern helps to decouple abstraction from implementation. With this if the implementation changes it does not affect abstraction and vice versa. Consider the figure ‘Abstraction and Implementation’. The switch is the abstraction and the electronic equipments are the implementations. The switch can be applied to any electronic equipment, so the switch is an abstract thinking while the equipments are implementations.

Figure: – Abstraction and Implementation

Let’s try to code the same switch and equipment example. First thing is we segregate the implementation and abstraction in to two different classes. Figure ‘Implementation’ shows how we have made an interface ‘IEquipment’ with ‘Start()’ and ‘Stop()’ methods. We have implemented two equipments one is the refrigerator and the other is the bulb.

Figure :- Implementation

The second part is the abstraction. Switch is the abstraction in our example. It has a ‘SetEquipment’ method which sets the object. The ‘On’ method calls the ‘Start’ method of the equipment and the ‘off’ calls the ‘stop’.

Figure: – Abstraction

Finally we see the client code. You can see we have created the implementation objects and the abstraction objects separately. We can use them in an isolated manner.

Figure :- Client code using bridge

——————————————————————————————————————————————————————

Readers are requested to add comment, and add more information in case they want.

Read more Quick and Short articles.

Leave a Reply

*