48 lines
1.5 KiB
Java
48 lines
1.5 KiB
Java
package bridge;
|
|
|
|
/**
|
|
* Bridge Design Pattern — Runnable Demo
|
|
*
|
|
* Shows how remotes (abstraction) and devices (implementation)
|
|
* can vary independently. 4 combinations from 2+2 classes,
|
|
* not 4 hard-coded classes.
|
|
*
|
|
* Run: javac bridge/*.java && java bridge.Main
|
|
* Article: https://ankurm.com/bridge-design-pattern-java/
|
|
*/
|
|
public class Main {
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println("=== Bridge Design Pattern Demo ===\n");
|
|
|
|
// Combination 1: Basic Remote + TV
|
|
System.out.println("-- Basic Remote controlling TV --");
|
|
RemoteControl remote1 = new RemoteControl(new TV());
|
|
remote1.togglePower();
|
|
remote1.volumeUp();
|
|
remote1.channelUp();
|
|
|
|
System.out.println();
|
|
|
|
// Combination 2: Advanced Remote + Radio
|
|
System.out.println("-- Advanced Remote controlling Radio --");
|
|
AdvancedRemote remote2 = new AdvancedRemote(new Radio());
|
|
remote2.togglePower();
|
|
remote2.volumeUp();
|
|
remote2.mute();
|
|
remote2.jumpToChannel(91);
|
|
|
|
System.out.println();
|
|
|
|
// Combination 3: Advanced Remote + TV (no new classes needed)
|
|
System.out.println("-- Advanced Remote controlling TV --");
|
|
AdvancedRemote remote3 = new AdvancedRemote(new TV());
|
|
remote3.togglePower();
|
|
remote3.jumpToChannel(5);
|
|
remote3.mute();
|
|
|
|
System.out.println("\n=== Demo complete ===");
|
|
System.out.println("3 different remote+device combinations, 0 new classes needed.");
|
|
}
|
|
}
|