The Bridge Pattern

DesignPattern Comments

This is the second post in a series of blogposts on Design Patterns. You can read about The Strategy Pattern here.

Intent

  1. The bridge pattern is used when we want to separate a varying entity from a varying behavior.
  2. The Gang of Four defines the Bridge Pattern as Separate an abstraction from its implementation so that the two can vary independently.

Example

Bridge Design Pattern

Assume that you’ve to write a program that draws a rectangle. Since you’ve 2 drawing libraries, you decide to create an abstract class called Rectangle and have the two versions extend this. But then after some time, we need to draw a circle too. So, we decide to create an abstract class called Shape and have both Rectangle and Circle extend it.

The problem with this approach is that scaling the behavior (drawing a line) also depends on scaling the entitities (rectangle, circle etc).

Structure

In this design pattern, the AbstractBehavior is passed as a parameter to AbstractEntity. This gives the AbstractBehavior to vary independently of the AbstractEntity.

Bridge Design Pattern

A way to achieve this procedurally is through nested branching statements - you can clearly see why this doesn’t scale.

if(entityConditionA) {
if(behaviorConditionA) {
// Algorithm A1;
} else {
// Algorithm A2;
}
} else {
if(behaviorCondition1) {
// Algorithm B1;
} else {
// Algorithm B2;
}
}

Code

public class AbstractEntity {
protected AbstractBehavior myBehavior;
public AbstractEntity(AbstractBehavior aBehavior) {
myBehavior = aBehavior;
}

public abstract void request();
}

public class ConcreteEntityA extends AbstractEntity {
public ConcreteEntityA(AbstractBehavior aBehavior) {
super(aBehavior);
}

public void request() {
myBehavior.operation1();
}
}

public class ConcreteEntityB extends AbstractEntity {
public ConcreteEntityB(AbstractBehavior aBehavior) {
super(aBehavior);
}

public void request() {
myBehavior.opration2();
}
}

public abstract class AbstractBehavior {
public abstract void operation1();
public abstract void operation2();
}

public class ConcreteBehaviorA extends AbstractBehavior {
public void operation1() {
// Behavior 1A
}

public void operation2() {
// Behavior 2A
}
}

public class ConcreteBehaviorB extends AbstractBehavior {
public void operation1() {
// Behavior 1B
}

public void operation2() {
// Behavior 2B
}
}

Kaushik Rangadurai

Code. Learn. Explore

Share this post

Comments