Bridge
Separate abstraction from implementation
Overview
The Bridge pattern is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently.
Key Concepts
Abstraction: High-level control layer, delegates work to the implementation
Refined Abstraction: Extends abstraction
Implementation: Interface for platform-specific implementations
Concrete Implementations: Actual platform-specific code
Uses composition over inheritance to prevent class explosion
Code Example
1// Implementation Interface
2public interface Device {
3 boolean isEnabled();
4 void enable();
5 void disable();
6 int getVolume();
7 void setVolume(int percent);
8}
9
10// Concrete Implementations
11public class Tv implements Device {
12 private boolean on = false;
13 private int volume = 30;
14
15 @Override public boolean isEnabled() { return on; }
16 @Override public void enable() { on = true; }
17 @Override public void disable() { on = false; }
18 @Override public int getVolume() { return volume; }
19 @Override public void setVolume(int percent) { this.volume = percent; }
20}
21
22public class Radio implements Device {
23 private boolean on = false;
24 private int volume = 10;
25
26 @Override public boolean isEnabled() { return on; }
27 @Override public void enable() { on = true; }
28 @Override public void disable() { on = false; }
29 @Override public int getVolume() { return volume; }
30 @Override public void setVolume(int percent) { this.volume = percent; }
31}
32
33// Abstraction
34public class RemoteControl {
35 protected Device device;
36
37 public RemoteControl(Device device) {
38 this.device = device;
39 }
40
41 public void togglePower() {
42 if (device.isEnabled()) device.disable();
43 else device.enable();
44 }
45
46 public void volumeDown() {
47 device.setVolume(device.getVolume() - 10);
48 }
49
50 public void volumeUp() {
51 device.setVolume(device.getVolume() + 10);
52 }
53}
54
55// Refined Abstraction
56public class AdvancedRemoteControl extends RemoteControl {
57 public AdvancedRemoteControl(Device device) {
58 super(device);
59 }
60
61 public void mute() {
62 device.setVolume(0);
63 }
64}
65
66// Client
67public class Main {
68 public static void main(String[] args) {
69 Device tv = new Tv();
70 RemoteControl remote = new RemoteControl(tv);
71 remote.togglePower();
72
73 Device radio = new Radio();
74 AdvancedRemoteControl advancedRemote = new AdvancedRemoteControl(radio);
75 advancedRemote.togglePower();
76 advancedRemote.mute();
77 }
78}The RemoteControl (Abstraction) holds a reference to a Device (Implementation). Instead of having TvRemote, RadioRemote, AdvancedTvRemote, we separate them into two independent hierarchies.
đź’ˇ Interview Tips
Explain how it prevents Cartesian product (class explosion) of subclasses.
Bridge is designed up-front to let the abstraction and implementation vary independently, unlike Adapter which is often used to make existing unrelated classes work together.