Bridge Design Pattern

The Bridge Pattern allows us to vary the abstraction and implementation independent of each other by placing the two in separate class hierarchies. It comes under structural design pattern as it decouples abstract class and implementation classes. Bridge pattern provides an interface which acts as a bridge between two class hierarchies.

Sometimes we confuse Bridge pattern with Adapter. Adapter pattern is used to communicate between two existing incompatible interfaces whereas Bridge is used to decouple client's code from implementation.

Advantages of Bridge Pattern

  • Abstraction and Implementation class hierarchies can be extended independently.
  • Decoupling of abstraction from implementation.
  • Client's code is decoupled with implementation. We can vary implementation without affecting the client code and the client code need not be compiled when we change implementation.
  • We can extend the abstraction and implementation class hierarchies independently.

When we should use Bridge pattern

  • When we want to decouple an abstraction from its implementation so that the two can vary independently.
  • When we want run-time binding of the implementation.
  • When we want to vary implementation without changing client's code.

Implementation of Bridge Design Pattern

We will use Bird as the interface for implementation classes and Crow.java and Duck.java as concrete implementation of Bird interface. Flyable is an abstract class which "HAS A" reference of Bird(implementer interface). FlyingCharacters are concrete implementation of Flyable abstract class. FlyingBirds.java is the client which uses FlyingCharacters class to create various flyable objects and move them using Bird Interface.

Bird.java
public interface Bird {
    public void makeSound();
    public void gotoPosition(int x, int y);
}

Crow.java and Duck.java are Concrete implementation of Bridge implementer interface.

Crow.java
public class Crow implements Bird {
    private String type;
    private int positionX;
    private int positionY;
 
    public Crow(String type){
        this.type = type;
    }
 
    @Override
    public void gotoPosition(int x, int y){
        System.out.println("Crow is moving to position (" + x + "," + y + ")");
        this.positionX = x;
        this.positionY = y;
    }
 
    @Override
    public void makeSound(){
        System.out.println("Caw Caw ....");
    }
}
Duck.java
public class Duck implements Bird {
    private String type;
    private int positionX;
    private int positionY;
 
    public Duck(String type){
        this.type = type;
    }
 
    @Override
    public void gotoPosition(int x, int y){
        System.out.println("Duck is moving to position (" + x + "," + y + ")");
        this.positionX = x;
        this.positionY = y;
    }
 
    @Override
    public void makeSound(){
        System.out.println("Quack Quack ....");
    }
}

Flyable is an abstract class which defines the abstraction interface.

Flyable.java
public abstract class Flyable {
    protected Bird bird;
 
    protected Flyable(Bird bird){
        this.bird = bird;
    } 
 
    public abstract void tweet();
    public abstract void move(int x, int y);
}
FlyingCharacters.java
public class FlyingCharacters extends Flyable {
    private int x, y;
 
    public FlyingCharacters(int x, int y, Bird bird){
        super(bird);
        this.x = x;
        this.y = y;
    }
 
    public void tweet(){
        bird.makeSound();
    }
 
    public void move(int x, int y){
        bird.gotoPosition(x, y);
        this.x = x;
        this.y = y;
    }
}
FlyingBirds.java
public class FlyingBirds {
    public static void main(String[] args) {
     Flyable duck = new FlyingCharacters(0, 0, new Duck("Duck"));
     Flyable crow = new FlyingCharacters(0, 0, new Crow("Crow"));
        System.out.println("Moving Birds");
     for(int i = 1; i < 5; i++){
         // Moving Duck
         duck.move(3*i, 3*i);
         // Moving Crow
         crow.move(4*i,  4*i);
         // Produce sound 
         duck.tweet();
         crow.tweet();
         System.out.println("----------------------");
     }
    }
}

Output

Moving Birds
Duck is moving to position (3,3)
Crow is moving to position (4,4)
Quack Quack ....
Caw Caw ....
----------------------
Duck is moving to position (6,6)
Crow is moving to position (8,8)
Quack Quack ....
Caw Caw ....
----------------------
Duck is moving to position (9,9)
Crow is moving to position (12,12)
Quack Quack ....
Caw Caw ....
----------------------
Duck is moving to position (12,12)
Crow is moving to position (16,16)
Quack Quack ....
Caw Caw ....
----------------------

Related Topics
Facade Design Pattern
Mediator Design Pattern
Interpreter Design Pattern
Factory Design Pattern
Abstract Factory Design Pattern
Singleton Design Pattern
Facade Design Pattern
Decorator Design Pattern
State Design Pattern
Memento Design Pattern
List of Design Patterns