Add all 23 GoF design pattern implementations

This commit is contained in:
2026-07-25 10:50:29 +05:30
commit f5688a6b32
164 changed files with 4371 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package bridge;
/**
* Refined Abstraction — extends RemoteControl with additional capabilities.
*
* The device hierarchy doesn't change at all. TV and Radio
* automatically support mute() and jumpToChannel() because they
* implement Device. Zero new code on the implementation side.
*/
public class AdvancedRemote extends RemoteControl {
public AdvancedRemote(Device device) {
super(device);
}
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);
}
}

View File

@@ -0,0 +1,16 @@
package bridge;
/**
* Implementor — the "implementation" side of the bridge.
* All devices (TV, Radio, SmartSpeaker, etc.) implement this.
* The remote controls only know about this interface, never about specific devices.
*/
public interface Device {
boolean isEnabled();
void enable();
void disable();
int getVolume();
void setVolume(int percent);
int getChannel();
void setChannel(int channel);
String getName();
}

View File

@@ -0,0 +1,29 @@
package bridge;
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.");
}
}

View File

@@ -0,0 +1,32 @@
# 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 -d out/bridge
java -cp out/bridge bridge.Main
```
Requires Java 25.
## Post Section ↔ File Mapping
| Post Section | File(s) |
|---|---|
| Step 1 — The Implementor Interface | `Device.java` |
| Step 2 — Concrete Implementors (TV and Radio) | `TV.java`, `Radio.java` |
| Step 3 — The Abstraction (RemoteControl) | `RemoteControl.java` |
| Step 4 — Refined Abstraction (AdvancedRemote) | `AdvancedRemote.java` |
| Wiring It Together: Main Demo | `Main.java` |
Note: the "Class Explosion Problem — Visualised" pseudocode and the JDBC/SLF4J snippets shown in the article are illustrative only — they are not part of this repository's runnable example.
Article: https://ankurm.com/bridge-design-pattern-java/
All patterns: https://ankurm.com/design-patterns-java/

View File

@@ -0,0 +1,18 @@
package bridge;
// Concrete Implementor #2: Radio
public class Radio implements Device {
private boolean on = false;
private int volume = 20;
private int channel = 1;
@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"; }
}

View File

@@ -0,0 +1,27 @@
package bridge;
/**
* Abstraction — the remote control.
*
* Key point: RemoteControl holds a Device (the bridge) and delegates
* all actual work to it. It adds higher-level semantics on top:
* "togglePower" instead of separate enable()/disable() calls.
*/
public class RemoteControl {
protected Device device; // THE BRIDGE
public RemoteControl(Device device) {
this.device = device;
System.out.println("Remote paired with: " + device.getName());
}
// User action → device operation translation
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); }
}

View File

@@ -0,0 +1,18 @@
package bridge;
// Concrete Implementor #1: Television
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"; }
}