The Bridge design pattern allows you to separate the abstraction from the implementation.
It is a
structural design pattern.
There are 2 parts in Bridge design pattern :
1. Abstraction
2. Implementation
Elements 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 implementers.
• Implementer – It defines the interface for implementation classes. This interface does not
need to correspond directly to the abstraction interface and can be very different.
Abstraction imp provides an implementation in terms of operations provided by the
Implementer interface.
• Concrete Implementation – Implements the above implementer by providing the concrete
implementation.
Advantages:
1. Bridge pattern decouple an abstraction from its implementation so that the two can vary
independently.
2. It is used mainly for implementing platform independence features.
3. It adds one more method level redirection to achieve the objective.
4. Publish abstraction interface in a separate inheritance hierarchy, and put the implementation
in its own inheritance hierarchy.
5. Use bridge pattern to run-time binding of the implementation.
6. Use bridge pattern to map orthogonal class hierarchies
7. Bridge is designed up-front to let the abstraction and the implementation vary
independently.
// Java code to demonstrate
// bridge design pattern
// abstraction in bridge pattern
abstract class Vehicle {
protected Workshop workShop1;
protected Workshop workShop2;
protected Vehicle(Workshop workShop1, Workshop workShop2)
this.workShop1 = workShop1;
this.workShop2 = workShop2;
abstract public void manufacture();
// Refine abstraction 1 in bridge pattern
class Car extends Vehicle {
public Car(Workshop workShop1, Workshop workShop2)
{
super(workShop1, workShop2);
@Override
public void manufacture()
[Link]("Car ");
[Link]();
[Link]();
// Refine abstraction 2 in bridge pattern
class Bike extends Vehicle {
public Bike(Workshop workShop1, Workshop workShop2)
super(workShop1, workShop2);
@Override
public void manufacture()
[Link]("Bike ");
[Link]();
[Link]();
// Implementer for bridge pattern
interface Workshop
{
abstract public void work();
// Concrete implementation 1 for bridge pattern
class Produce implements Workshop {
@Override
public void work()
[Link]("Produced");
// Concrete implementation 2 for bridge pattern
class Assemble implements Workshop {
@Override
public void work()
[Link](" And");
[Link](" Assembled.");
// Demonstration of bridge design pattern
class BridgePattern {
public static void main(String[] args)
Vehicle vehicle1 = new Car(new Produce(), new Assemble());
[Link]();
Vehicle vehicle2 = new Bike(new Produce(), new Assemble());
[Link]();
} }