Bridge Design Pattern
Bridge Design Pattern
Bridge Design Pattern
The Structural Design Patterns deal with different ways to create a class structure. Moreover, they use
Inheritance and Composition techniques to create larger objects from small objects.
When we have interface hierarchies in both interfaces as well as implementations, then the BRIDGE design pattern is used to
decouple the interfaces from implementation and hiding the implementation details from the client programs.
According to the GoF bridge design pattern is: Decouple an abstraction from its implementation so that the two can vary
independently.
In the Bridge Pattern, we separate an abstraction and its implementation and develop separate inheritance structures for both
the abstraction and the implementer. The abstraction is an interface or an abstract class, and likewise the implementer is an
interface or an abstract class. The abstraction contains a reference to the implementer. Children of the abstraction are referred to
as refined abstractions, and children of the implementer are concrete implementers. Since we can change the reference to the
implementer in the abstraction, we are able to change the abstraction’s implementer at run-time. However, changes to the
implementer do not affect client code.
The Bridge Pattern’s intent is to put the abstraction and implementation into two different class hierarchies so that both can be
extend independently.
Structural design patterns are concerned with how classes and objects can be composed, to form larger structures.
The structural design patterns simplifies the structure by identifying the relationships.
These patterns focus on, how the classes inherit from each other and how they are composed from other classes.
A Bridge Pattern says that just "decouple the functional abstraction from the implementation so that the two can vary
independently".
The Bridge Pattern is also known as Handle or Body.
redCircle.draw();
blackCircle.draw();
}
}
The code above generates the following result.
Implementation
We have a DrawAPI interface which is acting as a bridge implementer and concrete
classes RedCircle, GreenCircle implementing the DrawAPI interface. Shape is an abstract class and will use object
of DrawAPI. BridgePatternDemo, our demo class will use Shape class to draw different colored circle.
Step 1
Create bridge implementer interface.
DrawAPI.java
public interface DrawAPI
{
public void drawCircle(int radius, int x, int y);
}
Step 2
Create concrete bridge implementer classes implementing the DrawAPI interface.
RedCircle.java
public class RedCircle implements DrawAPI
{ @Override
public void drawCircle(int radius, int x, int y)
{
System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]");
}
}
GreenCircle.java
public class GreenCircle implements DrawAPI
{
@Override
public void drawCircle(int radius, int x, int y)
{
System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", " + y + "]");
}
}
Step 3
Create an abstract class Shape using the DrawAPI interface.
Shape.java