Structural Design Pattern
Bridge Design Pattern in Java
Separate an abstraction from its implementation so both can change independently.
In this lesson: The Bridge pattern separates a high-level abstraction from the implementation details that carry out its work. Both sides can evolve without creating a class for every combination.
Overview
Bridge is useful when its recurring design problem appears in a real application. It keeps the relevant responsibilities organized while allowing surrounding code to depend on clear abstractions.
Implementation
interface Device { void on(); }
final class Tv implements Device { public void on() { System.out.println("TV on"); } }
abstract class Remote { protected final Device device; protected Remote(Device device) { this.device = device; } abstract void powerOn(); }
final class BasicRemote extends Remote { BasicRemote(Device device) { super(device); } void powerOn() { device.on(); } }
Remote remote = new BasicRemote(new Tv());
remote.powerOn();
When should you use it?
Use Bridge when two dimensions of a design vary independently, such as remote controls and devices or reports and output formats.
Next step: Continue with the Design Patterns course and explore the next pattern.