Design Patterns - Bridge


// Design Patterns - Structural - Bridge:

Bridge is used when 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.

The Bridge pattern is designed to separate a class's interface from its 
implementation so you can vary or replace the implementation without changing 
the client code.

The Bridge pattern is an application of the old advice, "prefer composition over 
inheritance". It becomes handy when you must subclass different times in ways 
that are orthogonal with one another. Say you must implement a hierarchy of 
colored shapes. You wouldn't subclass Shape with Rectangle and Circle and then 
subclass Rectangle with RedRectangle, BlueRectangle and GreenRectangle and 
the same for Circle, would you? You would prefer to say that each Shape has a 
Color and to implement a hierarchy of colors, and that is the Bridge Pattern. 
Well, I wouldn't implement a "hierarchy of colors", but you get the idea...

Adapter and Bridge are certainly related, and the distinction is subtle. It's likely 
that some people who think they are using one of these patterns are actually 
using the other pattern.  Adapter is used when you're trying to unify the interfaces 
of some incompatible classes that already exist. The Adapter functions as a 
kind of translator to implementations that could be considered legacy.  Whereas 
the Bridge pattern is used for code that is more likely to be greenfield. You're 
designing the Bridge to provide an abstract interface for an implementation that 
needs to vary, but you also define the interface of those implementation classes.
Device drivers is an often-cited example of Bridge, but I'd say it's a Bridge if you're 
defining the interface spec for device vendors, but it's an Adapter if you're taking 
existing device drivers and making a wrapper-class to provide a unified interface.
So code-wise, the two patterns are very similar. Business-wise, they're different.

// Step 1: Create bridge implementer interface.
public interface DrawAPI {
   public void drawCircle(int radius, int x, int y);

// Step 2: Create concrete bridge implementer classes implementing the 
// DrawAPI interface.
public class RedCircle implements DrawAPI {
   public void drawCircle(int radius, int x, int y) {
      System.out.println("Drawing Circle[ color: red, radius: " + 
        radius + ", x: " + x + ", " + y + "]");

public class GreenCircle implements DrawAPI {
   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.
public abstract class Shape {
   protected DrawAPI drawAPI;

   protected Shape(DrawAPI drawAPI){
      this.drawAPI = drawAPI;
   public abstract void draw();    

// Step 4: Create concrete class extending the Shape class
public class Circle extends Shape {
   private int x, y, radius;

   public Circle(int x, int y, int radius, DrawAPI drawAPI) {
      this.x = x;  
      this.y = y;  
      this.radius = radius;

   public void draw() {

// Step 5: Use the Shape and DrawAPI classes to draw different colored circles
Shape redCircle = new Circle(100,100, 10, new RedCircle());
Shape greenCircle = new Circle(100,100, 10, new GreenCircle());

As you can see, the DrawAPI interface specifies the signature for the drawCircle
method.   The GreenCircle, and RedCircle classes implement the DrawAPI (implement
the drawCircle method.  The abstract class Shape defines a protected constructor
which accepts an object that implements the DrawAPI interface, and just store it.
The abstract class Shape also specify the signature for a draw method.  The Circle
class extends the Shape class, and its constructor take an object that implements
the DrawAPI interface, and pass it on to the super constructor to store it.  The
Circle class provide concrete implementation for the draw method, which in turn
invokes the drawCircle method of the DrawAPI object that was previously passed
to the constructor.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License