BRIDGE PATTERN

When we have interface hierarchies in both interfaces as well as implementations, then builder design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client programs.

According to 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 abstract class, and the implementer is likewise an interface or 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. Changes to the implementer do not affect client code.

The Bridge Pattern’s intent is to decouple an abstraction from its implementation so that the two can vary independently. It puts the abstraction and implementation into two different class hierarchies so that both can be extend independently.

                                                    BEFORE BRIDGE DESIGN PATTERN

                                                     AFTER BRIDGE DESIGN PATTERN

The adapter design pattern helps in making two incompatible classes to work together. But, bridge design pattern decouples the abstraction and implementation by creating two different hierarchies.

 

package com.myjavablog.structural.bridge;

//Abstraction in bridge pattern
public abstract class Vehicle {

protected VehicleType type1;
protected VehicleType type2;

public Vehicle(VehicleType type1, VehicleType type2) {
this.type1 = type1;
this.type2 = type2;
}

abstract public void purchase();
}

package com.myjavablog.structural.bridge;

//Refine abstraction-1 in bridge pattern
public class Car extends Vehicle {

public Car(VehicleType type1, VehicleType type2) {
super(type1, type2);
}

@Override
public void purchase() {
System.out.println("Car");
type1.book();
type2.book();
}
}

package com.myjavablog.structural.bridge;

//Refine abstraction-2 in bridge pattern
public class Bike extends Vehicle {

public Bike(VehicleType type1, VehicleType type2) {
super(type1, type2);
}

@Override
public void purchase() {
System.out.println("Bike");
type1.book();
type2.book();
}
}

 

package com.myjavablog.structural.bridge;

//Implementor for bridge pattern

public interface VehicleType {
public void book();
}

package com.myjavablog.structural.bridge;

//Concrete implementation 1 for bridge pattern
public class NewVehicle implements VehicleType {

@Override
public void book() {
System.out.println("New vehicle");
}
}

package com.myjavablog.structural.bridge;

//Concrete implementation 2 for bridge pattern
public class OldVehicle implements VehicleType {

@Override
public void book() {
System.out.println("Old vehicle");
}
}

 

package com.myjavablog.structural.bridge;

public class BridgePatternTest {

public static void main(String[] args) {

Vehicle v1 = new Car(new NewVehicle(), new OldVehicle());
v1.purchase();

Bike b1 = new Bike(new NewVehicle(), new OldVehicle());
b1.purchase();
}
}

 

Output:

Car
New vehicle
Old vehicle
Bike
New vehicle
Old vehicle

Summary of Bridge Design Pattern:

  • Creates two different hierarchies. One for abstraction and another for implementation.
  • Avoids permanent binding by removing the dependency between abstraction and implementation.
  • We create a bridge that coordinates between abstraction and implementation.
  • Abstraction and implementation can be extended separately.
  • Should be used when we have needed to switch implementation at runtime.
  • Client should not be impacted if there is modification in implementation of abstraction.
  • Best used when you have multiple implementations.

Usage in JDK:

  • AWT (It provides an abstraction layer which maps onto the native OS the windowing support.)
  • JDBC

Leave a Comment

Bitnami