Design Patterns - SPLessons

Bridge Pattern

Chapter 11

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Bridge Pattern

Bridge Pattern

shape Introduction

Bridge pattern is used to separate the interface from implementation and follows composition instead of inheritance.

shape Description

Bridge pattern is used when decoupling of interface from implementation should be done. By decoupling, both the interfaces can vary independently. The best example of bridge pattern is “electric bulb”. It has two functions-on and off. Bridge Patterns are also known as “handle” or “body”.

shape Advantages

  • Improves extensibility.
  • Implementation details can be hidden.
  • Functional interface and implementation can be linked easily.
  • Patterns are much flexible.
  • Implementation changes doesn’t effect the pattern.

shape Conceptual
figure

switch

shape Examples

Switch has two functions On and Off.

public interface Switch
{
public void switchOn();
public void switchOff();
}

fan

shape Examples

Fan can be operated by using switch.

public class Fan implements Switch
{
public void switchOn()
{
System.out.println("FAN Switched ON");
}
public void switchOff() {
System.out.println("FAN Switched OFF");
}
}

Electric bulb

shape Examples

Electric bulb can be operated using switch.

public class Bulb implements Switch
{
public void switchOn() {
System.out.println("BULB Switched ON");
}
public void switchOff()
{
System.out.println("BULB Switched OFF");
}
}

Television

shape Examples

Television can be operated using switch.

public class Television implements Switch {
public void switchOn()
{
System.out.println("Television Switched ON");
}
public void switchOff()
{
System.out.println(" Television Switched OFF");
}
}

Washing machine

shape Examples

Washing machine can be operated using switch.

public class WashingMachine implements Switch
 { 
public void switchOn() 
{
 System.out.println(" WashingMachine switched ON"); 
} 
public void switchOff() 
{
 System.out.println("WashingMachine Switched OFF"); 
} 
}

Summary

shape Key Points

  • Bridge Pattern are much flexible and can be extended easily without effecting the interface.
  • It improves the extensibility.
  • It allows the hiding of implementation details from the client.