Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
25
02-structural/bridge/AdvancedRemote.java
Normal file
25
02-structural/bridge/AdvancedRemote.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package bridge;
|
||||
|
||||
/**
|
||||
* Refined Abstraction — extends RemoteControl with extra features.
|
||||
* This is how you vary the "abstraction" side independently of the
|
||||
* "implementation" side. Both TV and Radio work with this remote,
|
||||
* even though they know nothing about it.
|
||||
*/
|
||||
public class AdvancedRemote extends RemoteControl {
|
||||
|
||||
public AdvancedRemote(Device device) {
|
||||
super(device);
|
||||
}
|
||||
|
||||
// Extra feature not in the basic remote
|
||||
public void mute() {
|
||||
System.out.println(" Muting " + device.getName());
|
||||
device.setVolume(0);
|
||||
}
|
||||
|
||||
public void jumpToChannel(int channel) {
|
||||
System.out.println(" Jumping to channel " + channel);
|
||||
device.setChannel(channel);
|
||||
}
|
||||
}
|
||||
17
02-structural/bridge/Device.java
Normal file
17
02-structural/bridge/Device.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package bridge;
|
||||
|
||||
/**
|
||||
* Implementor interface — the "implementation" side of the bridge.
|
||||
* This is what the Abstraction delegates its real work to.
|
||||
* TV, Radio, SmartSpeaker etc. all implement this.
|
||||
*/
|
||||
public interface Device {
|
||||
boolean isEnabled();
|
||||
void enable();
|
||||
void disable();
|
||||
int getVolume();
|
||||
void setVolume(int percent);
|
||||
int getChannel();
|
||||
void setChannel(int channel);
|
||||
String getName();
|
||||
}
|
||||
47
02-structural/bridge/Main.java
Normal file
47
02-structural/bridge/Main.java
Normal file
@@ -0,0 +1,47 @@
|
||||
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.");
|
||||
}
|
||||
}
|
||||
30
02-structural/bridge/README.md
Normal file
30
02-structural/bridge/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Bridge Design Pattern — Java Example
|
||||
|
||||
**Pattern:** Structural → Bridge
|
||||
**Article:** https://ankurm.com/bridge-design-pattern-java/
|
||||
|
||||
## What this example shows
|
||||
|
||||
Decouples remote controls (Abstraction) from devices (Implementation). A basic remote and an advanced remote each work with any device (TV, Radio) without creating N×M subclasses.
|
||||
|
||||
## How to run
|
||||
|
||||
```bash
|
||||
javac bridge/*.java
|
||||
java bridge.Main
|
||||
```
|
||||
|
||||
Requires Java 11+.
|
||||
|
||||
## Files
|
||||
|
||||
| File | Role |
|
||||
|---|---|
|
||||
| `Device.java` | Implementor interface |
|
||||
| `TV.java` / `Radio.java` | Concrete Implementors |
|
||||
| `RemoteControl.java` | Abstraction (holds Device bridge) |
|
||||
| `AdvancedRemote.java` | Refined Abstraction |
|
||||
| `Main.java` | Demo entry point |
|
||||
|
||||
Article: https://ankurm.com/bridge-design-pattern-java/
|
||||
All patterns: https://ankurm.com/design-patterns-java/
|
||||
29
02-structural/bridge/Radio.java
Normal file
29
02-structural/bridge/Radio.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package bridge;
|
||||
|
||||
/**
|
||||
* Concrete Implementor — a radio.
|
||||
* Same Device interface, completely different internal behaviour.
|
||||
*/
|
||||
public class Radio implements Device {
|
||||
|
||||
private boolean on = false;
|
||||
private int volume = 20;
|
||||
private int channel = 1; // FM frequency index simplified
|
||||
|
||||
@Override public boolean isEnabled() { return on; }
|
||||
@Override public void enable() { on = true; System.out.println(" [Radio] Powered ON"); }
|
||||
@Override public void disable() { on = false; System.out.println(" [Radio] Powered OFF"); }
|
||||
|
||||
@Override
|
||||
public int getVolume() { return volume; }
|
||||
|
||||
@Override
|
||||
public void setVolume(int percent) {
|
||||
this.volume = Math.max(0, Math.min(100, percent));
|
||||
System.out.println(" [Radio] Volume set to " + this.volume);
|
||||
}
|
||||
|
||||
@Override public int getChannel() { return channel; }
|
||||
@Override public void setChannel(int ch) { this.channel = ch; System.out.println(" [Radio] Frequency -> " + ch); }
|
||||
@Override public String getName() { return "JBL Radio"; }
|
||||
}
|
||||
33
02-structural/bridge/RemoteControl.java
Normal file
33
02-structural/bridge/RemoteControl.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package bridge;
|
||||
|
||||
/**
|
||||
* Abstraction — the remote control. It holds a reference to a Device
|
||||
* (the "bridge") and delegates all real work to it.
|
||||
*
|
||||
* The key: RemoteControl doesn't care whether it talks to a TV or Radio.
|
||||
* It holds a Device and calls Device methods. That's the bridge.
|
||||
*/
|
||||
public class RemoteControl {
|
||||
|
||||
// The bridge — link from Abstraction to Implementation
|
||||
protected Device device;
|
||||
|
||||
public RemoteControl(Device device) {
|
||||
this.device = device;
|
||||
System.out.println("Remote paired with: " + device.getName());
|
||||
}
|
||||
|
||||
public void togglePower() {
|
||||
if (device.isEnabled()) {
|
||||
device.disable();
|
||||
} else {
|
||||
device.enable();
|
||||
}
|
||||
}
|
||||
|
||||
public void volumeUp() { device.setVolume(device.getVolume() + 10); }
|
||||
public void volumeDown() { device.setVolume(device.getVolume() - 10); }
|
||||
|
||||
public void channelUp() { device.setChannel(device.getChannel() + 1); }
|
||||
public void channelDown() { device.setChannel(device.getChannel() - 1); }
|
||||
}
|
||||
29
02-structural/bridge/TV.java
Normal file
29
02-structural/bridge/TV.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package bridge;
|
||||
|
||||
/**
|
||||
* Concrete Implementor — a television.
|
||||
* Contains device-specific logic for a TV.
|
||||
*/
|
||||
public class TV implements Device {
|
||||
|
||||
private boolean on = false;
|
||||
private int volume = 30;
|
||||
private int channel = 1;
|
||||
|
||||
@Override public boolean isEnabled() { return on; }
|
||||
@Override public void enable() { on = true; System.out.println(" [TV] Powered ON"); }
|
||||
@Override public void disable() { on = false; System.out.println(" [TV] Powered OFF"); }
|
||||
|
||||
@Override
|
||||
public int getVolume() { return volume; }
|
||||
|
||||
@Override
|
||||
public void setVolume(int percent) {
|
||||
this.volume = Math.max(0, Math.min(100, percent));
|
||||
System.out.println(" [TV] Volume set to " + this.volume);
|
||||
}
|
||||
|
||||
@Override public int getChannel() { return channel; }
|
||||
@Override public void setChannel(int ch) { this.channel = ch; System.out.println(" [TV] Channel -> " + ch); }
|
||||
@Override public String getName() { return "Samsung TV"; }
|
||||
}
|
||||
Reference in New Issue
Block a user