Lesson 5
Bridge Pattern
Bridge Pattern

Lastly - The Bridge Design Pattern! The Bridge pattern is a structural design pattern that decouples an abstraction from its implementation, allowing both to evolve independently. This pattern is particularly useful when you want to avoid a permanent binding between an abstraction and its concrete implementation.

What You Will Learn

In this lesson, you will:

  • Understand the Bridge pattern and its purpose.
  • Learn how to implement the Bridge pattern through a detailed example with multiple components.
  • See how the Bridge pattern helps in creating flexible and scalable systems.

Let's dive into the Bridge pattern through a practical example.

Implementing the Bridge Pattern

The goal of the Bridge pattern is to decouple the abstraction from its implementation, allowing both to evolve independently and providing greater flexibility and scalability.

We will be implementing this pattern with devices like Printer and Scanner, which will run on different operating systems like Windows and MacOS.

Step 1: Define the Abstraction Layer

First, let's define the OperatingSystem interface. This interface will declare the run method, which will be implemented by different operating systems.

OperatingSystem.java:

Java
1public interface OperatingSystem { 2 void run(String device); 3}

In this interface, the run method takes a device name and performs an operation on it.

Step 2: Implement Concrete Implementors

Next, we need concrete classes that implement the OperatingSystem interface. These classes will perform specific operations based on the operating system.

WindowsOS.java:

Java
1public class WindowsOS implements OperatingSystem { 2 @Override 3 public void run(String device) { 4 System.out.println("Running " + device + " on Windows OS."); 5 } 6}

MacOS.java:

Java
1public class MacOS implements OperatingSystem { 2 @Override 3 public void run(String device) { 4 System.out.println("Running " + device + " on MacOS."); 5 } 6}

These classes implement the run method for Windows and MacOS, respectively.

Step 3: Create the Abstraction Class

Now, we create the abstract class Device, which will hold a reference to an OperatingSystem. The Device class will then define an abstract method start.

Device.java:

Java
1public abstract class Device { 2 protected OperatingSystem os; 3 4 public Device(OperatingSystem os) { 5 this.os = os; 6 } 7 8 public abstract void start(); 9}

Here, the Device class has a constructor to set the operating system and an abstract method start to be defined by subclasses.

Step 4: Implement Refined Abstractions

Finally, we create concrete classes for specific devices, which will extend the Device class and implement the start method.

Printer.java:

Java
1public class Printer extends Device { 2 public Printer(OperatingSystem os) { 3 super(os); 4 } 5 6 @Override 7 public void start() { 8 System.out.print("Printer: "); 9 os.run("Printer"); 10 } 11}

Scanner.java:

Java
1public class Scanner extends Device { 2 public Scanner(OperatingSystem os) { 3 super(os); 4 } 5 6 @Override 7 public void start() { 8 System.out.print("Scanner: "); 9 os.run("Scanner"); 10 } 11}

These classes implement the start method to run the device on the specified operating system.

Step 5: Putting It All Together

Now, let's bring everything together in the Main class and test our implementation.

Main.java:

Java
1public class Main { 2 public static void main(String[] args) { 3 // Running a Printer on Windows OS 4 Device printerOnWindows = new Printer(new WindowsOS()); 5 printerOnWindows.start(); // Outputs: Printer: Running Printer on Windows OS. 6 7 // Running a Scanner on MacOS 8 Device scannerOnMac = new Scanner(new MacOS()); 9 scannerOnMac.start(); // Outputs: Scanner: Running Scanner on MacOS. 10 } 11}

In the Main class, we create instances of Printer and Scanner with different operating systems and start these devices.

Importance and Benefits

The Bridge pattern is crucial for several reasons:

  • Decoupling Abstraction from Implementation: It allows both the abstraction and the implementation to change independently without affecting each other.
  • Increased Flexibility: By treating the implementation and the abstraction as separate class hierarchies, the system becomes more flexible and easier to expand.
  • Reduced Complexity: This pattern helps manage complex systems by breaking down the class hierarchies and minimizing the connections between them.

Understanding and implementing the Bridge pattern enables you to design systems that are more flexible, scalable, and maintainable.

Ready to solidify your understanding with hands-on practice? Let's proceed to the practice section!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.