When designing software systems, it is essential to have a clean and maintainable code. One way to achieve this is by decoupling abstractions, separating them from their implementation details. The Bridge Pattern is a design pattern that allows us to do this effectively. In this article, we will explore what the Bridge Pattern is and how to use it in Java.

What is the Bridge Pattern?

The Bridge Pattern is a structural design pattern that decouples an abstraction from its implementation so that the two can vary independently. It is useful when you want to avoid a permanent binding between an abstraction and its implementation. Instead, you can create a bridge between them, which allows you to change the implementation without affecting the abstraction.

In the Bridge Pattern, you have two hierarchies: the Abstraction hierarchy and the Implementation hierarchy. The Abstraction hierarchy defines the interface for the client, while the Implementation hierarchy provides the implementation details. The Bridge acts as a link between the two hierarchies, providing a way for the client to access the implementation details indirectly.

How to Use Bridge Pattern in Java

To implement the Bridge Pattern in Java, you need to follow a few steps:

  1. Define the Abstraction hierarchy: This hierarchy should define the abstract interface that the client will use. It should be implemented by a Concrete Abstraction class that uses the Bridge to access the implementation details.

  2. Define the Implementation hierarchy: This hierarchy should provide the implementation details. It should be implemented by a Concrete Implementation class that implements the interface defined by the Abstraction hierarchy.

  3. Define the Bridge: This class acts as a link between the Abstraction and Implementation hierarchies. It should contain a reference to the implementation object and provide methods for the client to access the implementation details indirectly.

  4. Use the Bridge: Finally, you can use the Bridge to decouple the abstraction from its implementation. The client can interact with the Abstraction hierarchy through the Bridge, which will use the Concrete Implementation to provide the implementation details.

Example Code:

public interface Vehicle {
    void startEngine();
}

public class Car implements Vehicle {
    @Override
    public void startEngine() {
        System.out.println("Starting car engine.");
    }
}

public class Bike implements Vehicle {
    @Override
    public void startEngine() {
        System.out.println("Starting bike engine.");
    }
}

public abstract class VehicleType {
    protected Vehicle vehicle;

    public VehicleType(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    public abstract void start();
}

public class TwoWheeler extends VehicleType {
    public TwoWheeler(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public void start() {
        vehicle.startEngine();
    }
}

public class FourWheeler extends VehicleType {
    public FourWheeler(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public void start() {
        vehicle.startEngine();
    }
}

public class Client {
    public static void main(String[] args) {
        Vehicle car = new Car();
        Vehicle bike = new Bike();

        VehicleType twoWheeler = new TwoWheeler(bike);
        VehicleType fourWheeler = new FourWheeler(car);

        twoWheeler.start();
        fourWheeler.start();
    }
}

In this example, we have an Abstraction hierarchy defined by the VehicleType abstract class, which is implemented by the TwoWheeler and FourWheeler classes. The Implementation hierarchy is defined by the Vehicle interface, which is implemented by the Car and Bike classes. The Bridge is formed by the VehicleType class, which contains a reference to the Vehicle object and provides a way for the client to access the implementation details indirectly.

The Bridge Pattern is a powerful tool for decoupling abstractions from their implementation details. It allows you to change the implementation without affecting the abstraction, making your code more maintainable and flexible. By following the steps outlined in this article, you can easily implement the Bridge Pattern in your Java projects.

Reference : The Bridge Pattern in Java: An Effective Approach to Decoupling Abstractions

+ Recent posts