Design Pattern

Bridge Pattern

Clean Java-only production-ready implementation.


What Problem It Solves

When you have TWO orthogonal dimensions of variation. Example: different Remote types (Basic, Advanced) and different Device types (TV, Radio, Projector). Without Bridge, you'd create classes like BasicTVRemote, AdvancedTVRemote, BasicRadioRemote, AdvancedRadioRemote โ€” combinatorial explosion. Bridge separates abstraction (remote) from implementation (device).

Bridge vs Adapter

AspectBridgeAdapter
PurposeDecouple abstraction from implementationMake incompatible interfaces work together
WhenDesigned upfront to allow variation on both sidesUsed to integrate existing/legacy code
StructureAbstraction HAS-A implementationClient expects Target, Adapter wraps Adaptee

Implementation

// โ”€โ”€โ”€ EXAMPLE 1 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// WHAT WE ARE IMPLEMENTING:
// A smart home network coupling multiple remote controllers with various home
// TVs.
//
// WHERE THE PATTERN FITS IN:
// RemoteControl is the Abstraction. TV is the Implementation interface.
// Concrete classes like SonyTV and SamsungTV implement it independently.
// โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
// --- Implementation interface ---
interface Device {
    void powerOn();
    void powerOff();
    void setVolume(int percent);
}

// --- Concrete implementations ---
class TV implements Device {
    public void powerOn() { System.out.println("  [TV] Powered on"); }
    public void powerOff() { System.out.println("  [TV] Powered off"); }
    public void setVolume(int percent) { System.out.println("  [TV] Volume set to " + percent); }
}

class Radio implements Device {
    public void powerOn() { System.out.println("  [Radio] Powered on"); }
    public void powerOff() { System.out.println("  [Radio] Powered off"); }
    public void setVolume(int percent) { System.out.println("  [Radio] Volume set to " + percent); }
}

// --- Abstraction (uses Device via bridge) ---
abstract class Remote {
    protected Device device;
    public Remote(Device device) { this.device = device; }
    abstract void togglePower();
    abstract void volumeUp();
    abstract void volumeDown();
}

class BasicRemote extends Remote {
    private boolean isOn = false;
    private int volume = 50;

    public BasicRemote(Device device) { super(device); }

    void togglePower() {
        if (isOn) { device.powerOff(); isOn = false; }
        else { device.powerOn(); isOn = true; }
    }

    void volumeUp() { device.setVolume(++volume); }
    void volumeDown() { device.setVolume(--volume); }
}

class AdvancedRemote extends Remote {
    private boolean isOn = false;
    private int volume = 50;

    public AdvancedRemote(Device device) { super(device); }

    void togglePower() { isOn = !isOn; System.out.println(isOn ? "  [Remote] On" : "  [Remote] Off"); }
    void volumeUp() { device.setVolume(++volume); }
    void volumeDown() { device.setVolume(--volume); }
    void mute() { device.setVolume(0); volume = 0; }
}

public class Main {
    public static void main(String[] args) {
        // Abstraction (Remote) decoupled from Implementation (Device)
        Remote basicTV = new BasicRemote(new TV());
        Remote advancedRadio = new AdvancedRemote(new Radio());

        basicTV.togglePower();     // TV powered on
        basicTV.volumeUp();        // TV volume 51

        advancedRadio.togglePower();
        advancedRadio.volumeUp();  // Radio volume 51
    }
}

๐Ÿ’ฌReview

Help Us Improve

How helpful was this walkthrough?

Click a star to rate. We actively use this feedback to refine and update our system design content.

Placeholder
Optional but highly appreciated!

Discussion

Share your thoughts, ask questions, or help others.

Loading comments...