-
Notifications
You must be signed in to change notification settings - Fork 10
Bridge Design Pattern
Here we are going to learn about Bridge pattern, it is structural design pattern.
Bridge is used where we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern comes under structural pattern as this pattern decouples implementation class and abstract class by providing a bridge structure between them. This pattern involves an interface which acts as a bridge which makes the functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other.
When there are inheritance hierarchies creating concrete implementation, you loose flexibility because of interdependence.Decouple implementation from interface and hiding implementation details from client is the essense of bridge design pattern
- Abstraction – core of the bridge design pattern and defines the crux. Contains a reference to the implementer.
- Refined Abstraction – Extends the abstraction takes the finer detail one level below. Hides the finer elements from implemetors.
- Implementer – This interface is the higer level than abstraction. Just defines the basic operations.
- Concrete Implementation – Implements the above implementer by providing concrete implementation.
Example included in Java project.
Step 1. Creating Implementor : Create bridge implementer interface as IDraw.
public interface IDraw {
public void drawColor( );
}
Step 2 Concrete Implementation: Create concrete bridge implementer classes implementing the IDraw interface.
public class GreenColor implements IDraw{
@Override
public void drawColor() {
// TODO Auto-generated method stub
System.out.println("Green Color");
}
}
public class RedColor implements IDraw{
@Override
public void drawColor() {
// TODO Auto-generated method stub
System.out.println("Red Color");
}
}
Step 3 Abstraction : Create an abstract class Shape using the IDraw interface.
public abstract class Shape {
protected IDraw iDraw;
protected Shape(IDraw drawAPI) {
this.iDraw = drawAPI;
}
public abstract void draw();
}
Step 4 Refined Abstraction : Create concrete class Circle and Rectangle extending Shape
public class Circle extends Shape {
protected Circle(IDraw iDraw) {
super(iDraw);
}
@Override
public void draw() {
iDraw.drawColor();
}
}
public class Rectangleextends Shape {
protected Rectangle(IDraw iDraw) {
super(iDraw);
}
@Override
public void draw() {
iDraw.drawColor();
}
}
Step 5 Bridge Pattern Demo : Use the Shape and IDraw classes to draw different color Shapes.
public class BridgePatternDemo {
public static void main(String[] args) {
Shape rect = new Rectangle(new RedColor());
rect.draw();
Shape circle = new Circle(new GreenColor());
circle.draw();
}
}
The adapter design pattern helps it two incompatible classes to work together. But, bridge design pattern decouples the abstraction and implementation by creating two different hierarchies.