Design Patterns - SPLessons
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. [java]public interface Switch { public void switchOn(); public void switchOff(); }[/java]

fan

shape Examples

Fan can be operated by using switch. [java] public class Fan implements Switch { public void switchOn() { System.out.println("FAN Switched ON"); } public void switchOff() { System.out.println("FAN Switched OFF"); } }[/java]

Electric bulb

shape Examples

Electric bulb can be operated using switch. [java] public class Bulb implements Switch { public void switchOn() { System.out.println("BULB Switched ON"); } public void switchOff() { System.out.println("BULB Switched OFF"); } }[/java]

Television

shape Examples

Television can be operated using switch. [java] public class Television implements Switch { public void switchOn() { System.out.println("Television Switched ON"); } public void switchOff() { System.out.println(" Television Switched OFF"); } }[/java]

Washing machine

shape Examples

Washing machine can be operated using switch. [java] public class WashingMachine implements Switch { public void switchOn() { System.out.println(" WashingMachine switched ON"); } public void switchOff() { System.out.println("WashingMachine Switched OFF"); } }[/java]

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.