Easy Tutorial
❮ Service Locator Pattern Business Delegate Pattern ❯

Bridge Pattern

Bridge is used to decouple abstraction from implementation, allowing both to vary independently. This type of design pattern is a structural pattern, which provides a bridge structure between abstraction and implementation to decouple them.

This pattern involves an interface acting as a bridge, which makes the functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other.

We demonstrate the usage of the Bridge Pattern with the following example. Here, the same abstract class methods can be used with different bridge implementer classes to draw circles of different colors.

Introduction

Intent: Decouple an abstraction from its implementation so that the two can vary independently.

Main Problem: Using inheritance in scenarios with multiple potential changes leads to a class explosion problem and inflexible extensions.

When to Use: When the system might be categorized from multiple perspectives, each of which may change.

Solution: Separate these multi-perspective categorizations, allowing them to vary independently, and reduce their coupling.

Key Code: The abstract class depends on the implementation class.

Example Applications: 1. Pigsy's reincarnation from Marshal Tian Peng to a pig. The reincarnation mechanism divides the mortal world into two levels: soul and body, the former being abstraction and the latter being implementation. Creatures delegate functionality to the body object, allowing dynamic selection.

  1. Wall switches, where the visible switch is abstract, without needing to know the specific internal implementation.

Advantages: 1. Separation of abstraction and implementation.

  1. Excellent extensibility.
  2. Implementation details are transparent to the client.

Disadvantages: The introduction of the Bridge Pattern increases system comprehension and design complexity. Since the aggregation association is established at the abstract level, developers are required to design and program against abstraction.

Usage Scenarios: 1. When a system needs more flexibility between its abstract and concrete components to avoid static inheritance links, the Bridge Pattern can establish an association at the abstract level.

  1. It is particularly suitable for systems where inheritance is not desired or where multi-level inheritance leads to a rapid increase in the number of system classes.
  2. When a class has two independently changing dimensions that both need extension.

Notes: The Bridge Pattern is most suitable for two independently changing dimensions.

Implementation

We have a DrawAPI interface acting as a bridge implementer, and concrete classes RedCircle, GreenCircle implementing the DrawAPI interface. Shape is an abstract class that uses the DrawAPI object. The BridgePatternDemo class uses the Shape class to draw circles of different colors.

Step 1

Create a bridge implementer interface.

DrawAPI.java

public interface DrawAPI {
   public void drawCircle(int radius, int x, int y);
}

Step 2

Create concrete bridge implementer classes that implement the DrawAPI interface.

RedCircle.java

public class RedCircle implements DrawAPI {
   @Override
   public void drawCircle(int radius, int x, int y) {
      System.out.println("Drawing Circle[ color: red, radius: "
         + radius +", x: " +x+", "+ y +"]");
   }
}

GreenCircle.java

public class GreenCircle implements DrawAPI {
   @Override
   public void drawCircle(int radius, int x, int y) {
      System.out.println("Drawing Circle[ color: green, radius: "
         + radius +", x: " +x+", "+ y +"]");
   }
}

Step 3

Create an abstract class Shape using the DrawAPI interface.

Shape.java

public abstract class Shape {
   protected DrawAPI drawAPI;
   protected Shape(DrawAPI drawAPI){
      this.drawAPI = drawAPI;
   }
   public abstract void draw();  
}

Step 4

Create concrete classes implementing the Shape abstract class.

Circle.java

public class Circle extends Shape {
   private int x, y, radius;

   public Circle(int x, int y, int radius, DrawAPI drawAPI) {
      super(drawAPI);
      this.x = x;  
      this.y = y;  
      this.radius = radius;
   }

   public void draw() {
      drawAPI.drawCircle(radius,x,y);
   }
}

Step 5

Use the Shape and DrawAPI classes to draw circles of different colors.

BridgePatternDemo.java

public class BridgePatternDemo {
   public static void main(String[] args) {
      Shape redCircle = new Circle(100,100, 10, new RedCircle());
      Shape greenCircle = new Circle(100,100, 10, new GreenCircle());

      redCircle.draw();
      greenCircle.draw();
   }
}

Step 6

Execute the program, outputting the result:

Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]

Recommended Articles

Bridge Pattern

❮ Service Locator Pattern Business Delegate Pattern ❯