Design Pattern

Bridge Pattern

Clean Java-only production-ready implementation.


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).

// ─── 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
    }
}