Master Software Architecture & Clean Code!
Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!
SUBSCRIBE NOWWhat is the Bridge Design Pattern?
The Bridge Design Pattern is a structural pattern whose main goal is to decouple an abstraction from its implementation so that both can vary independently. Instead of relying heavily on inheritance—which often leads to an explosion of subclasses—the Bridge pattern prefers object composition.
In this LED TV example, the user control interface (like a remote control) acts as the abstraction, while the actual TV hardware (Sony, Samsung, LG) acts as the implementation.
Complete Java Code Implementation
Below is a clean, practical implementation illustrating the Bridge pattern using an LED TV and remote control hierarchy:
// 1. Implementor Interface
interface LedTv {
void on();
void off();
void switchChannel(int channel);
}
// 2. Concrete Implementors
class SonyLedTv implements LedTv {
@Override
public void on() {
System.out.println("Sony TV is turned ON.");
}
@Override
public void off() {
System.out.println("Sony TV is turned OFF.");
}
@Override
public void switchChannel(int channel) {
System.out.println("Sony TV tuned to channel: " + channel);
}
}
class SamsungLedTv implements LedTv {
@Override
public void on() {
System.out.println("Samsung TV is turned ON.");
}
@Override
public void off() {
System.out.println("Samsung TV is turned OFF.");
}
@Override
public void switchChannel(int channel) {
System.out.println("Samsung TV tuned to channel: " + channel);
}
}
// 3. Abstraction (Bridge container)
abstract class RemoteControl {
protected LedTv tv;
public RemoteControl(LedTv tv) {
this.tv = tv;
}
public abstract void turnOn();
public abstract void turnOff();
public abstract void setChannel(int channel);
}
// 4. Refined Abstraction
class BasicRemoteControl extends RemoteControl {
public BasicRemoteControl(LedTv tv) {
super(tv);
}
@Override
public void turnOn() {
tv.on();
}
@Override
public void turnOff() {
tv.off();
}
@Override
public void setChannel(int channel) {
tv.switchChannel(channel);
}
}
// 5. Client Application
public class BridgePatternDemo {
public static void main(String[] args) {
// Control Sony TV
LedTv sonyTv = new SonyLedTv();
RemoteControl sonyRemote = new BasicRemoteControl(sonyTv);
sonyRemote.turnOn();
sonyRemote.setChannel(5);
sonyRemote.turnOff();
System.out.println("--------------------");
// Control Samsung TV using the same abstraction
LedTv samsungTv = new SamsungLedTv();
RemoteControl samsungRemote = new BasicRemoteControl(samsungTv);
samsungRemote.turnOn();
samsungRemote.setChannel(12);
samsungRemote.turnOff();
}
}
Key Architectural Advantages
- Decoupled Hierarchies: Both abstraction (Remotes) and implementation (TV brands) can grow independently without cross-affecting code.
- Eliminates Class Explosion: Avoids creating permutations like
SonyBasicRemote,SamsungBasicRemote,SonySmartRemote, etc. - Runtime Flexibility: Concrete implementations can be swapped easily at runtime through constructor or setter injection.
Watch More Related Design Pattern Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=iCSrIzVSZwM
Click the below Image to Enlarge
| Bridge Design pattern - Implementation [LED TV] |
| Bridge Design pattern - Implementation [LED TV] - Class Diagram |
LEDTV.java
public interface LEDTV
{
public void switchOn();
public void switchOff();
public void setChannel(int channelNumber);
}
SamsungLedTv.java
public class SamsungLedTv implements LEDTV
{
@Override
public void switchOn()
{
System.out.println("Turning ON.. Samsung TV.");
}
@Override
public void switchOff()
{
System.out.println("Turning Off.. Samsung TV.");
}
@Override
public void setChannel( int channelNumber )
{
System.out.println("Setting channel Number " + channelNumber + ".. on Samsung TV");
}
}
SonyLedTv.java
public class SonyLedTv implements LEDTV
{
@Override
public void switchOn()
{
System.out.println("Turning ON.. Sony TV.");
}
@Override
public void switchOff()
{
System.out.println("Turning Off.. Sony TV.");
}
@Override
public void setChannel( int channelNumber )
{
System.out.println("Setting channel Number " + channelNumber + ".. on Sony TV");
}
}
AbstractRemoteControl.java
abstract class AbstractRemoteControl
{
protected LEDTV ledTv;
protected AbstractRemoteControl( LEDTV ledTv )
{
this.ledTv = ledTv;
}
public abstract void switchOn();
public abstract void switchOff();
public abstract void setChannel( int channelNumber );
}
{
public SonyRemoteControl( LEDTV ledtv )
{
super(ledtv);
}
@Override
public void switchOn()
{
ledTv.switchOn();
}
@Override
public void switchOff()
{
ledTv.switchOff();
}
@Override
public void setChannel( int channelNumber )
{
ledTv.setChannel(channelNumber);
}
}