Master Software Architecture & Clean Code!
Subscribe to Ram N Java for clean code, design pattern implementations, and practical Java tutorials!
SUBSCRIBE NOWUnderstanding the Bridge Pattern with TV & Remote
The Bridge Design Pattern separates an abstraction from its implementation so that both can evolve independently. Instead of relying on rigid, deep inheritance hierarchies that combine every remote control type with every television brand, the Bridge pattern decouples the two using object composition.
In this classic real-time example, the Remote Control acts as the abstraction layer (defining user operations), while the TV device serves as the implementation layer (executing brand-specific commands).
Complete Java Code Walkthrough
Here is an end-to-end, clean implementation showing how the TV device implementation connects to the remote abstraction:
// 1. Implementor Interface
interface TV {
void on();
void off();
void tuneChannel(int channel);
}
// 2. Concrete Implementors
class SonyTV implements TV {
@Override
public void on() {
System.out.println("Sony TV is switched ON.");
}
@Override
public void off() {
System.out.println("Sony TV is switched OFF.");
}
@Override
public void tuneChannel(int channel) {
System.out.println("Sony TV tuned to channel: " + channel);
}
}
class LGTV implements TV {
@Override
public void on() {
System.out.println("LG TV is switched ON.");
}
@Override
public void off() {
System.out.println("LG TV is switched OFF.");
}
@Override
public void tuneChannel(int channel) {
System.out.println("LG TV tuned to channel: " + channel);
}
}
// 3. Abstraction (Bridge holding the Implementor reference)
abstract class Remote {
protected TV tv;
public Remote(TV tv) {
this.tv = tv;
}
public abstract void powerOn();
public abstract void powerOff();
public abstract void changeChannel(int channel);
}
// 4. Refined Abstraction
class SmartRemote extends Remote {
public SmartRemote(TV tv) {
super(tv);
}
@Override
public void powerOn() {
tv.on();
}
@Override
public void powerOff() {
tv.off();
}
@Override
public void changeChannel(int channel) {
tv.tuneChannel(channel);
}
}
// 5. Client Application
public class TVBridgeDemo {
public static void main(String[] args) {
// Control a Sony TV with SmartRemote
TV sony = new SonyTV();
Remote remote = new SmartRemote(sony);
remote.powerOn();
remote.changeChannel(7);
remote.powerOff();
System.out.println("--------------------");
// Switch to an LG TV using the same abstraction
TV lg = new LGTV();
Remote lgRemote = new SmartRemote(lg);
lgRemote.powerOn();
lgRemote.changeChannel(21);
lgRemote.powerOff();
}
}
Key Architectural Benefits
- Avoids Class Explosion: Prevents having to build distinct subclasses for every combination of remote and manufacturer (e.g.,
SonySmartRemote,LGSmartRemote). - Loose Coupling: Abstraction logic and device implementations can be updated, extended, or tested independently.
- Runtime Swapping: Client code can reassign or swap underlying TV implementations on the fly without breaking remote functionality.
Watch More Related Design Pattern Tutorials
Click here to watch in Youtube : https://www.youtube.com/watch?v=qOVAIVt1dkA
Click the below Image to Enlarge
| Bridge Design pattern - Real time example [TV] |
No comments:
Post a Comment