Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
38
02-structural/adapter/Main.java
Normal file
38
02-structural/adapter/Main.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* Adapter Design Pattern — Runnable Demo
|
||||
*
|
||||
* Demonstrates wrapping an incompatible StripeClient behind
|
||||
* the PaymentGateway interface your application expects.
|
||||
*
|
||||
* Run: javac adapter/*.java && java adapter.Main
|
||||
* Article: https://ankurm.com/adapter-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Adapter Design Pattern Demo ===\n");
|
||||
|
||||
// --- Object Adapter: Stripe ---
|
||||
System.out.println("-- Using Stripe via Adapter --");
|
||||
StripeClient stripeClient = new StripeClient("sk_test_4eC39HqLyjWDarjtT1zdp7dc");
|
||||
PaymentGateway stripeGateway = new StripePaymentAdapter(stripeClient);
|
||||
|
||||
OrderService orderService = new OrderService(stripeGateway);
|
||||
orderService.processOrder("ORD-001", "cus_abc123", 99.99);
|
||||
|
||||
System.out.println("\n-- Testing refund --");
|
||||
boolean refunded = stripeGateway.refund("ch_ORD-001", 99.99);
|
||||
System.out.println("Refund issued: " + refunded);
|
||||
|
||||
// --- JDK Example: InputStreamReader as Adapter ---
|
||||
System.out.println("\n-- JDK Adapter: InputStreamReader --");
|
||||
// InputStreamReader adapts InputStream (byte-based) to Reader (char-based)
|
||||
// The client (BufferedReader) only knows about Reader, not InputStream
|
||||
System.out.println("InputStreamReader wraps System.in (InputStream) as a Reader.");
|
||||
System.out.println("Your code reads chars; the adapter handles byte-to-char conversion.");
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
31
02-structural/adapter/OrderService.java
Normal file
31
02-structural/adapter/OrderService.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* The Client — uses only the PaymentGateway interface.
|
||||
* It has no idea whether it's talking to Stripe, PayPal, or Braintree.
|
||||
* This is the point: the client is completely isolated from the vendor.
|
||||
*/
|
||||
public class OrderService {
|
||||
|
||||
private final PaymentGateway gateway;
|
||||
|
||||
// Receives a PaymentGateway — could be Stripe, PayPal, anything
|
||||
public OrderService(PaymentGateway gateway) {
|
||||
this.gateway = gateway;
|
||||
}
|
||||
|
||||
public void processOrder(String orderId, String customerId, double total) {
|
||||
System.out.printf("%nProcessing order %s for customer %s, total: $%.2f%n",
|
||||
orderId, customerId, total);
|
||||
|
||||
boolean charged = gateway.charge(customerId, total, "USD");
|
||||
|
||||
if (charged) {
|
||||
System.out.println("Payment accepted. Order confirmed.");
|
||||
String status = gateway.getStatus("ch_" + orderId);
|
||||
System.out.println("Transaction status: " + status);
|
||||
} else {
|
||||
System.out.println("Payment failed. Order rejected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
12
02-structural/adapter/PaymentGateway.java
Normal file
12
02-structural/adapter/PaymentGateway.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* The Target interface — what YOUR application's code expects.
|
||||
* Every payment gateway in your system must implement this.
|
||||
* Written to handle modern async-style payment flows.
|
||||
*/
|
||||
public interface PaymentGateway {
|
||||
boolean charge(String customerId, double amount, String currency);
|
||||
boolean refund(String transactionId, double amount);
|
||||
String getStatus(String transactionId);
|
||||
}
|
||||
33
02-structural/adapter/README.md
Normal file
33
02-structural/adapter/README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Adapter Design Pattern — Java Example
|
||||
|
||||
**Pattern:** Structural → Adapter
|
||||
**Article:** https://ankurm.com/adapter-design-pattern-java/
|
||||
|
||||
## What this example shows
|
||||
|
||||
Wraps a third-party `StripeClient` (with its own API) behind a `PaymentGateway` interface that your application code expects. The `OrderService` client never touches `StripeClient` directly — it only sees `PaymentGateway`. Swapping payment providers requires changing one line.
|
||||
|
||||
## How to run
|
||||
|
||||
```bash
|
||||
# From this folder:
|
||||
javac adapter/*.java
|
||||
java adapter.Main
|
||||
```
|
||||
|
||||
Requires Java 11+.
|
||||
|
||||
## Files
|
||||
|
||||
| File | Role |
|
||||
|---|---|
|
||||
| `PaymentGateway.java` | Target interface (what your app expects) |
|
||||
| `StripeClient.java` | Adaptee (third-party SDK you can't modify) |
|
||||
| `StripePaymentAdapter.java` | Adapter (bridges the two) |
|
||||
| `OrderService.java` | Client (only uses PaymentGateway) |
|
||||
| `Main.java` | Demo entry point |
|
||||
|
||||
## See Also
|
||||
|
||||
- Full article: https://ankurm.com/adapter-design-pattern-java/
|
||||
- All design patterns: https://ankurm.com/design-patterns-java/
|
||||
51
02-structural/adapter/StripeClient.java
Normal file
51
02-structural/adapter/StripeClient.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* The Adaptee — a third-party payment SDK with a completely different interface.
|
||||
* Imagine this is Stripe's actual SDK: you cannot modify this class,
|
||||
* and it doesn't implement PaymentGateway.
|
||||
*
|
||||
* In real projects, this would be a JAR you depend on.
|
||||
*/
|
||||
public class StripeClient {
|
||||
|
||||
private final String apiKey;
|
||||
|
||||
public StripeClient(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
System.out.println("[Stripe] Initialized with key: " + apiKey.substring(0, 8) + "...");
|
||||
}
|
||||
|
||||
// Stripe uses cents, not decimal amounts
|
||||
public StripeChargeResult createCharge(String customerId, long amountInCents, String currency) {
|
||||
System.out.printf("[Stripe] Charging customer=%s, amount=%d cents, currency=%s%n",
|
||||
customerId, amountInCents, currency);
|
||||
// Simulate success
|
||||
return new StripeChargeResult("ch_" + System.currentTimeMillis(), true, null);
|
||||
}
|
||||
|
||||
// Stripe's refund method takes a charge ID and uses different naming
|
||||
public boolean issueRefund(String chargeId, long amountInCents) {
|
||||
System.out.printf("[Stripe] Refunding charge=%s, amount=%d cents%n", chargeId, amountInCents);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Stripe uses 'retrieve' not 'getStatus', and returns an object
|
||||
public StripeChargeResult retrieveCharge(String chargeId) {
|
||||
System.out.printf("[Stripe] Retrieving charge=%s%n", chargeId);
|
||||
return new StripeChargeResult(chargeId, true, "succeeded");
|
||||
}
|
||||
|
||||
// Stripe-specific result object — nothing in common with your domain
|
||||
public static class StripeChargeResult {
|
||||
public final String chargeId;
|
||||
public final boolean success;
|
||||
public final String status;
|
||||
|
||||
public StripeChargeResult(String chargeId, boolean success, String status) {
|
||||
this.chargeId = chargeId;
|
||||
this.success = success;
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
02-structural/adapter/StripePaymentAdapter.java
Normal file
44
02-structural/adapter/StripePaymentAdapter.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* The Adapter — bridges StripeClient (Adaptee) to PaymentGateway (Target).
|
||||
*
|
||||
* This is the Object Adapter variant: it holds a StripeClient instance
|
||||
* via composition (not inheritance), so it can adapt any StripeClient
|
||||
* including subclasses.
|
||||
*
|
||||
* The key responsibility: translate YOUR interface's methods into
|
||||
* calls that Stripe understands — data conversion included.
|
||||
*/
|
||||
public class StripePaymentAdapter implements PaymentGateway {
|
||||
|
||||
private final StripeClient stripe;
|
||||
|
||||
public StripePaymentAdapter(StripeClient stripe) {
|
||||
this.stripe = stripe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean charge(String customerId, double amount, String currency) {
|
||||
// Translation: your code uses decimal dollars; Stripe wants integer cents
|
||||
long amountInCents = Math.round(amount * 100);
|
||||
StripeClient.StripeChargeResult result =
|
||||
stripe.createCharge(customerId, amountInCents, currency.toLowerCase());
|
||||
return result.success;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean refund(String transactionId, double amount) {
|
||||
// Translation: your "transactionId" is Stripe's "chargeId"
|
||||
long amountInCents = Math.round(amount * 100);
|
||||
return stripe.issueRefund(transactionId, amountInCents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatus(String transactionId) {
|
||||
// Translation: map Stripe's object to your simple status string
|
||||
StripeClient.StripeChargeResult result = stripe.retrieveCharge(transactionId);
|
||||
if (!result.success) return "FAILED";
|
||||
return result.status != null ? result.status.toUpperCase() : "UNKNOWN";
|
||||
}
|
||||
}
|
||||
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"; }
|
||||
}
|
||||
53
02-structural/composite/Directory.java
Normal file
53
02-structural/composite/Directory.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package composite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Composite — a directory that can hold both Files (leaves)
|
||||
* and other Directories (composites).
|
||||
*
|
||||
* getSize() is recursive: it asks each child for its size and sums them.
|
||||
* The caller doesn't care whether a child is a File or Directory —
|
||||
* both implement FileSystemItem and answer getSize().
|
||||
*
|
||||
* This is the power of Composite: uniform treatment of simple and complex.
|
||||
*/
|
||||
public class Directory implements FileSystemItem {
|
||||
|
||||
private final String name;
|
||||
private final List<FileSystemItem> children = new ArrayList<>();
|
||||
|
||||
public Directory(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Directory add(FileSystemItem item) {
|
||||
children.add(item);
|
||||
return this; // fluent API for easy nesting
|
||||
}
|
||||
|
||||
public void remove(FileSystemItem item) {
|
||||
children.remove(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() { return name; }
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
// Recursion: each child knows its own size.
|
||||
// Files return their bytes; directories sum their children.
|
||||
return children.stream()
|
||||
.mapToLong(FileSystemItem::getSize)
|
||||
.sum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String indent) {
|
||||
System.out.printf("%s[DIR] %s/ (%,d bytes total)%n", indent, name, getSize());
|
||||
for (FileSystemItem child : children) {
|
||||
child.print(indent + " "); // recurse with deeper indent
|
||||
}
|
||||
}
|
||||
}
|
||||
27
02-structural/composite/File.java
Normal file
27
02-structural/composite/File.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package composite;
|
||||
|
||||
/**
|
||||
* Leaf — a single file. It has no children.
|
||||
* getSize() returns its own size. print() shows its name.
|
||||
*
|
||||
* Notice: the File has no knowledge of directories or nesting.
|
||||
* It just knows its own name and size.
|
||||
*/
|
||||
public class File implements FileSystemItem {
|
||||
|
||||
private final String name;
|
||||
private final long size;
|
||||
|
||||
public File(String name, long sizeBytes) {
|
||||
this.name = name;
|
||||
this.size = sizeBytes;
|
||||
}
|
||||
|
||||
@Override public String getName() { return name; }
|
||||
@Override public long getSize() { return size; }
|
||||
|
||||
@Override
|
||||
public void print(String indent) {
|
||||
System.out.printf("%s[FILE] %s (%,d bytes)%n", indent, name, size);
|
||||
}
|
||||
}
|
||||
12
02-structural/composite/FileSystemItem.java
Normal file
12
02-structural/composite/FileSystemItem.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package composite;
|
||||
|
||||
/**
|
||||
* Component interface — the common contract for BOTH files (leaves)
|
||||
* and directories (composites). Clients work through this interface
|
||||
* and never need to know which they're dealing with.
|
||||
*/
|
||||
public interface FileSystemItem {
|
||||
String getName();
|
||||
long getSize(); // total size in bytes (recursive for directories)
|
||||
void print(String indent); // display the tree
|
||||
}
|
||||
58
02-structural/composite/Main.java
Normal file
58
02-structural/composite/Main.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package composite;
|
||||
|
||||
/**
|
||||
* Composite Design Pattern — Runnable Demo
|
||||
*
|
||||
* Builds a file system tree with nested directories and files.
|
||||
* Demonstrates that getSize() and print() work uniformly on
|
||||
* leaves (File) and composites (Directory) without any instanceof checks.
|
||||
*
|
||||
* Run: javac composite/*.java && java composite.Main
|
||||
* Article: https://ankurm.com/composite-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Composite Design Pattern Demo ===\n");
|
||||
|
||||
// Build a file system tree
|
||||
Directory root = new Directory("project");
|
||||
|
||||
Directory src = new Directory("src");
|
||||
Directory main = new Directory("main");
|
||||
main.add(new File("App.java", 4_200))
|
||||
.add(new File("Config.java", 1_800))
|
||||
.add(new File("Application.yml", 3_100));
|
||||
|
||||
Directory test = new Directory("test");
|
||||
test.add(new File("AppTest.java", 2_600))
|
||||
.add(new File("ConfigTest.java", 1_200));
|
||||
|
||||
src.add(main).add(test);
|
||||
|
||||
Directory resources = new Directory("resources");
|
||||
resources.add(new File("application.yml", 1_500))
|
||||
.add(new File("logback.xml", 900))
|
||||
.add(new File("banner.txt", 200));
|
||||
|
||||
root.add(src)
|
||||
.add(resources)
|
||||
.add(new File("pom.xml", 8_400))
|
||||
.add(new File("README.md", 2_100));
|
||||
|
||||
// Print entire tree — recursion happens automatically
|
||||
System.out.println("File system tree:");
|
||||
root.print("");
|
||||
|
||||
System.out.printf("%nTotal project size: %,d bytes%n", root.getSize());
|
||||
|
||||
// Client treats File and Directory identically
|
||||
System.out.println("\n-- Treating File and Directory uniformly --");
|
||||
FileSystemItem[] items = { new File("standalone.txt", 500), src };
|
||||
for (FileSystemItem item : items) {
|
||||
System.out.printf("%s -> size: %,d bytes%n", item.getName(), item.getSize());
|
||||
}
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
53
02-structural/decorator/Main.java
Normal file
53
02-structural/decorator/Main.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package decorator;
|
||||
|
||||
/**
|
||||
* Decorator Design Pattern — Runnable Demo
|
||||
*
|
||||
* Shows how text processors can be stacked like Java IO streams.
|
||||
* Each decorator adds one behaviour; the order of wrapping matters.
|
||||
*
|
||||
* Run: javac decorator/*.java && java decorator.Main
|
||||
* Article: https://ankurm.com/decorator-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Decorator Design Pattern Demo ===\n");
|
||||
|
||||
String input = " hello world, badword is here ";
|
||||
System.out.println("Input: \"" + input + "\"");
|
||||
System.out.println();
|
||||
|
||||
// Stack 1: just trim
|
||||
TextProcessor trimOnly = new TrimDecorator(new PlainTextProcessor());
|
||||
System.out.println("Trim only: \"" + trimOnly.process(input) + "\"");
|
||||
|
||||
// Stack 2: trim, then upper case
|
||||
TextProcessor trimThenUpper =
|
||||
new UpperCaseDecorator(
|
||||
new TrimDecorator(
|
||||
new PlainTextProcessor()));
|
||||
System.out.println("Trim + UpperCase: \"" + trimThenUpper.process(input) + "\"");
|
||||
|
||||
// Stack 3: trim, filter profanity, then upper case
|
||||
TextProcessor full =
|
||||
new UpperCaseDecorator(
|
||||
new ProfanityFilterDecorator(
|
||||
new TrimDecorator(
|
||||
new PlainTextProcessor())));
|
||||
System.out.println("Trim + Filter + Upper: \"" + full.process(input) + "\"");
|
||||
|
||||
// Stack 4: different order — filter then trim (order matters!)
|
||||
TextProcessor filterFirst =
|
||||
new TrimDecorator(
|
||||
new ProfanityFilterDecorator(
|
||||
new PlainTextProcessor()));
|
||||
System.out.println("Filter + Trim: \"" + filterFirst.process(input) + "\"");
|
||||
|
||||
System.out.println();
|
||||
System.out.println("JDK parallel: new BufferedReader(new InputStreamReader(socket.getInputStream()))");
|
||||
System.out.println("Same pattern: each wrapper adds one behaviour, order matters.");
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
14
02-structural/decorator/PlainTextProcessor.java
Normal file
14
02-structural/decorator/PlainTextProcessor.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package decorator;
|
||||
|
||||
/**
|
||||
* Concrete Component — the base object being decorated.
|
||||
* Does nothing special: just returns the text as-is.
|
||||
* All decorators wrap this (or other decorators on top of it).
|
||||
*/
|
||||
public class PlainTextProcessor implements TextProcessor {
|
||||
|
||||
@Override
|
||||
public String process(String text) {
|
||||
return text; // base: no transformation
|
||||
}
|
||||
}
|
||||
20
02-structural/decorator/ProfanityFilterDecorator.java
Normal file
20
02-structural/decorator/ProfanityFilterDecorator.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package decorator;
|
||||
|
||||
/** Concrete Decorator 3: replaces bad words with asterisks. */
|
||||
public class ProfanityFilterDecorator extends TextDecorator {
|
||||
|
||||
private static final String[] BAD_WORDS = {"badword", "spam"};
|
||||
|
||||
public ProfanityFilterDecorator(TextProcessor wrapped) {
|
||||
super(wrapped);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String process(String text) {
|
||||
String result = super.process(text);
|
||||
for (String word : BAD_WORDS) {
|
||||
result = result.replaceAll("(?i)" + word, "*".repeat(word.length()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
25
02-structural/decorator/TextDecorator.java
Normal file
25
02-structural/decorator/TextDecorator.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package decorator;
|
||||
|
||||
/**
|
||||
* Base Decorator — holds a reference to the wrapped TextProcessor.
|
||||
* All concrete decorators extend this instead of implementing
|
||||
* TextProcessor directly. This avoids repeating the delegation
|
||||
* boilerplate in every decorator.
|
||||
*
|
||||
* Crucially: it delegates to the wrapped processor first,
|
||||
* then applies its own transformation to the result.
|
||||
*/
|
||||
public abstract class TextDecorator implements TextProcessor {
|
||||
|
||||
protected final TextProcessor wrapped;
|
||||
|
||||
protected TextDecorator(TextProcessor wrapped) {
|
||||
this.wrapped = wrapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String process(String text) {
|
||||
// Delegate to the wrapped processor first, then let subclass apply its transform
|
||||
return wrapped.process(text);
|
||||
}
|
||||
}
|
||||
10
02-structural/decorator/TextProcessor.java
Normal file
10
02-structural/decorator/TextProcessor.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package decorator;
|
||||
|
||||
/**
|
||||
* Component interface — defines what all text processors do.
|
||||
* Both the concrete processor AND all decorators implement this.
|
||||
* This is what makes them stackable.
|
||||
*/
|
||||
public interface TextProcessor {
|
||||
String process(String text);
|
||||
}
|
||||
14
02-structural/decorator/TrimDecorator.java
Normal file
14
02-structural/decorator/TrimDecorator.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package decorator;
|
||||
|
||||
/** Concrete Decorator 2: trims leading/trailing whitespace. */
|
||||
public class TrimDecorator extends TextDecorator {
|
||||
|
||||
public TrimDecorator(TextProcessor wrapped) {
|
||||
super(wrapped);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String process(String text) {
|
||||
return super.process(text).trim();
|
||||
}
|
||||
}
|
||||
16
02-structural/decorator/UpperCaseDecorator.java
Normal file
16
02-structural/decorator/UpperCaseDecorator.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package decorator;
|
||||
|
||||
/** Concrete Decorator 1: converts all text to upper case. */
|
||||
public class UpperCaseDecorator extends TextDecorator {
|
||||
|
||||
public UpperCaseDecorator(TextProcessor wrapped) {
|
||||
super(wrapped);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String process(String text) {
|
||||
// Get the result from whatever is below us in the stack,
|
||||
// then apply OUR transformation on top of it.
|
||||
return super.process(text).toUpperCase();
|
||||
}
|
||||
}
|
||||
30
02-structural/facade/Main.java
Normal file
30
02-structural/facade/Main.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package facade;
|
||||
|
||||
/**
|
||||
* Facade Design Pattern — Runnable Demo
|
||||
*
|
||||
* Demonstrates reducing a complex video conversion subsystem
|
||||
* to a single method call via a Facade.
|
||||
*
|
||||
* Run: javac facade/*.java && java facade.Main
|
||||
* Article: https://ankurm.com/facade-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Facade Design Pattern Demo ===\n");
|
||||
|
||||
VideoConversionFacade converter = new VideoConversionFacade();
|
||||
|
||||
System.out.println("-- Client: just one method call --");
|
||||
String result1 = converter.convertVideo("holiday.ogg", "mp4");
|
||||
System.out.println("Output: " + result1);
|
||||
|
||||
System.out.println();
|
||||
String result2 = converter.convertVideo("presentation.mp4", "ogg");
|
||||
System.out.println("Output: " + result2);
|
||||
|
||||
System.out.println("\nClient code: 1 line. Subsystem: 6 classes. Facade hides the complexity.");
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
62
02-structural/facade/Subsystems.java
Normal file
62
02-structural/facade/Subsystems.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package facade;
|
||||
|
||||
/**
|
||||
* Complex subsystem classes — these are what the Facade hides.
|
||||
* Each class has its own complex API; clients shouldn't need to know all of them.
|
||||
*/
|
||||
|
||||
class VideoFile {
|
||||
private final String filename;
|
||||
private final String codecType;
|
||||
|
||||
VideoFile(String filename) {
|
||||
this(filename, filename.endsWith(".mp4") ? "mpeg4" : "ogg");
|
||||
}
|
||||
|
||||
VideoFile(String filename, String codec) {
|
||||
this.filename = filename;
|
||||
this.codecType = codec;
|
||||
System.out.println(" VideoFile: " + filename + " [codec: " + codecType + "]");
|
||||
}
|
||||
|
||||
public String getFilename() { return filename; }
|
||||
public String getCodecType() { return codecType; }
|
||||
}
|
||||
|
||||
interface Codec { String getName(); }
|
||||
|
||||
class MPEG4CompressionCodec implements Codec {
|
||||
@Override public String getName() { return "mpeg4"; }
|
||||
}
|
||||
|
||||
class OggCompressionCodec implements Codec {
|
||||
@Override public String getName() { return "ogg"; }
|
||||
}
|
||||
|
||||
class CodecFactory {
|
||||
static Codec extract(VideoFile file) {
|
||||
System.out.println(" CodecFactory: extracting codec from " + file.getFilename());
|
||||
if ("mpeg4".equals(file.getCodecType())) return new MPEG4CompressionCodec();
|
||||
return new OggCompressionCodec();
|
||||
}
|
||||
}
|
||||
|
||||
class BitrateReader {
|
||||
static VideoFile read(VideoFile file, Codec codec) {
|
||||
System.out.println(" BitrateReader: reading " + file.getFilename()
|
||||
+ " with codec " + codec.getName());
|
||||
return new VideoFile(file.getFilename());
|
||||
}
|
||||
|
||||
static VideoFile convert(VideoFile buffer, Codec codec) {
|
||||
System.out.println(" BitrateReader: converting to " + codec.getName());
|
||||
return new VideoFile(buffer.getFilename());
|
||||
}
|
||||
}
|
||||
|
||||
class AudioMixer {
|
||||
static VideoFile fix(VideoFile result) {
|
||||
System.out.println(" AudioMixer: fixing audio tracks");
|
||||
return new VideoFile(result.getFilename());
|
||||
}
|
||||
}
|
||||
39
02-structural/facade/VideoConversionFacade.java
Normal file
39
02-structural/facade/VideoConversionFacade.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package facade;
|
||||
|
||||
/**
|
||||
* Facade — the single, simple entry point to a complex video subsystem.
|
||||
*
|
||||
* Without this class, clients need to know about CodecFactory,
|
||||
* BitrateReader, AudioMixer, and VideoFile — 4 classes, dozens of methods.
|
||||
* The facade reduces that to ONE method call.
|
||||
*
|
||||
* The subsystem classes still exist and can be used directly
|
||||
* by advanced users who need fine-grained control.
|
||||
*/
|
||||
public class VideoConversionFacade {
|
||||
|
||||
public String convertVideo(String inputFile, String targetFormat) {
|
||||
System.out.println("VideoConversionFacade: starting conversion of " + inputFile);
|
||||
|
||||
// Step 1: open the file and detect codec
|
||||
VideoFile file = new VideoFile(inputFile);
|
||||
Codec sourceCodec = CodecFactory.extract(file);
|
||||
|
||||
// Step 2: prepare destination codec
|
||||
Codec destCodec;
|
||||
if ("mp4".equals(targetFormat)) {
|
||||
destCodec = new MPEG4CompressionCodec();
|
||||
} else {
|
||||
destCodec = new OggCompressionCodec();
|
||||
}
|
||||
|
||||
// Step 3: read, mix audio, encode
|
||||
VideoFile buffer = BitrateReader.read(file, sourceCodec);
|
||||
VideoFile intermediateResult = BitrateReader.convert(buffer, destCodec);
|
||||
VideoFile result = AudioMixer.fix(intermediateResult);
|
||||
|
||||
String outputFilename = inputFile.replaceAll("\\.[^.]+$", "." + targetFormat);
|
||||
System.out.println("VideoConversionFacade: conversion complete -> " + outputFilename);
|
||||
return outputFilename;
|
||||
}
|
||||
}
|
||||
50
02-structural/flyweight/Main.java
Normal file
50
02-structural/flyweight/Main.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package flyweight;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Flyweight Design Pattern — Runnable Demo
|
||||
*
|
||||
* Creates 1000 trees of only 3 species. Without Flyweight: 1000 TreeType
|
||||
* objects. With Flyweight: 3 TreeType objects (one per species), shared.
|
||||
*
|
||||
* Run: javac flyweight/*.java && java flyweight.Main
|
||||
* Article: https://ankurm.com/flyweight-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Flyweight Design Pattern Demo ===\n");
|
||||
|
||||
List<Tree> forest = new ArrayList<>();
|
||||
Random rnd = new Random(42);
|
||||
|
||||
String[][] treeSpecs = {
|
||||
{"Oak", "dark-green", "rough-bark"},
|
||||
{"Pine", "blue-green", "needle-texture"},
|
||||
{"Birch","light-green","smooth-white-bark"}
|
||||
};
|
||||
|
||||
System.out.println("Creating 1,000 trees (only 3 TreeType objects should be created):");
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
String[] spec = treeSpecs[i % 3];
|
||||
TreeType type = TreeFactory.getTreeType(spec[0], spec[1], spec[2]);
|
||||
forest.add(new Tree(rnd.nextInt(800), rnd.nextInt(600), type));
|
||||
}
|
||||
|
||||
System.out.println("\nForest created. Drawing first 5 trees:");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
forest.get(i).draw();
|
||||
}
|
||||
|
||||
System.out.println("\n--- Memory summary ---");
|
||||
System.out.println("Trees in forest : " + forest.size());
|
||||
System.out.println("Unique TreeType objects in pool: " + TreeFactory.getPoolSize());
|
||||
System.out.println("Without Flyweight : 1,000 TreeType objects");
|
||||
System.out.println("With Flyweight : " + TreeFactory.getPoolSize() + " TreeType objects shared");
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
31
02-structural/flyweight/Tree.java
Normal file
31
02-structural/flyweight/Tree.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package flyweight;
|
||||
|
||||
/**
|
||||
* Context — stores the UNIQUE (extrinsic) state for each tree instance.
|
||||
* This is NOT the flyweight itself; it's the lightweight object that
|
||||
* holds position data and delegates rendering to a shared TreeType.
|
||||
*
|
||||
* 10,000 Tree objects × (x:4 bytes + y:4 bytes + reference:8 bytes) = ~160 KB
|
||||
* vs.
|
||||
* 10,000 Tree objects × (name + color + texture + x + y) = potentially MBs
|
||||
*/
|
||||
public class Tree {
|
||||
|
||||
// Extrinsic (unique) state — different per tree
|
||||
private final int x;
|
||||
private final int y;
|
||||
|
||||
// Reference to the SHARED flyweight
|
||||
private final TreeType type;
|
||||
|
||||
public Tree(int x, int y, TreeType type) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
// Passes extrinsic state (position) into the shared flyweight
|
||||
type.draw(x, y);
|
||||
}
|
||||
}
|
||||
29
02-structural/flyweight/TreeFactory.java
Normal file
29
02-structural/flyweight/TreeFactory.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package flyweight;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Flyweight Factory — the cache that ensures each unique TreeType
|
||||
* is only created once, no matter how many trees use it.
|
||||
*
|
||||
* This is the piece that makes Flyweight work:
|
||||
* it intercepts creation requests and returns an existing
|
||||
* shared instance if one already exists.
|
||||
*/
|
||||
public class TreeFactory {
|
||||
|
||||
// The pool of shared flyweights
|
||||
private static final Map<String, TreeType> treeTypes = new HashMap<>();
|
||||
|
||||
public static TreeType getTreeType(String name, String color, String texture) {
|
||||
String key = name + "_" + color + "_" + texture;
|
||||
|
||||
// Only create a new TreeType if we haven't seen this combination before
|
||||
return treeTypes.computeIfAbsent(key, k -> new TreeType(name, color, texture));
|
||||
}
|
||||
|
||||
public static int getPoolSize() {
|
||||
return treeTypes.size();
|
||||
}
|
||||
}
|
||||
29
02-structural/flyweight/TreeType.java
Normal file
29
02-structural/flyweight/TreeType.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package flyweight;
|
||||
|
||||
/**
|
||||
* Flyweight — holds the SHARED (intrinsic) state.
|
||||
* TreeType is shared between all trees of the same species.
|
||||
*
|
||||
* If you have 10,000 oak trees, there's ONE OakType object in memory.
|
||||
* Each individual tree only stores its unique position (extrinsic state).
|
||||
*/
|
||||
public class TreeType {
|
||||
|
||||
// Intrinsic (shared) state — same for all trees of this species
|
||||
private final String name;
|
||||
private final String color;
|
||||
private final String texture; // imagine a large texture bitmap here
|
||||
|
||||
public TreeType(String name, String color, String texture) {
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.texture = texture;
|
||||
System.out.println(" [TreeType created: " + name + "]"); // see how few are created
|
||||
}
|
||||
|
||||
// Extrinsic state (x, y) is passed IN at render time — NOT stored here
|
||||
public void draw(int x, int y) {
|
||||
System.out.printf(" Drawing %s tree [%s/%s] at (%d, %d)%n",
|
||||
name, color, texture, x, y);
|
||||
}
|
||||
}
|
||||
11
02-structural/proxy/DatabaseConnection.java
Normal file
11
02-structural/proxy/DatabaseConnection.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package proxy;
|
||||
|
||||
/**
|
||||
* Subject interface — defines what both the real object and proxy expose.
|
||||
* Clients depend on this, not on the concrete class.
|
||||
*/
|
||||
public interface DatabaseConnection {
|
||||
void connect();
|
||||
String executeQuery(String sql);
|
||||
void disconnect();
|
||||
}
|
||||
52
02-structural/proxy/LazyConnectionProxy.java
Normal file
52
02-structural/proxy/LazyConnectionProxy.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package proxy;
|
||||
|
||||
/**
|
||||
* Virtual Proxy — delays creating the RealDatabaseConnection until
|
||||
* the first actual query is made. If no query is ever made (e.g.,
|
||||
* the service is initialized but never used in this request),
|
||||
* the expensive connection is never opened.
|
||||
*
|
||||
* This is exactly how Hibernate proxies work: entities are not
|
||||
* loaded from the database until you access a field on them.
|
||||
*/
|
||||
public class LazyConnectionProxy implements DatabaseConnection {
|
||||
|
||||
private final String url;
|
||||
private RealDatabaseConnection real; // null until first use
|
||||
|
||||
public LazyConnectionProxy(String url) {
|
||||
this.url = url;
|
||||
System.out.println("[Proxy] Created for " + url + " (real connection NOT opened yet)");
|
||||
}
|
||||
|
||||
// Lazy initialization — create and connect only on first real need
|
||||
private void initIfNeeded() {
|
||||
if (real == null) {
|
||||
System.out.println("[Proxy] First access — initializing real connection...");
|
||||
real = new RealDatabaseConnection(url);
|
||||
real.connect();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {
|
||||
// Proxy absorbs the connect() call — real connection opened lazily
|
||||
System.out.println("[Proxy] connect() called — deferring to first query");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeQuery(String sql) {
|
||||
initIfNeeded(); // NOW we actually need the connection
|
||||
return real.executeQuery(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
if (real != null) {
|
||||
real.disconnect();
|
||||
real = null;
|
||||
} else {
|
||||
System.out.println("[Proxy] disconnect() called but connection was never opened");
|
||||
}
|
||||
}
|
||||
}
|
||||
41
02-structural/proxy/LoggingProxy.java
Normal file
41
02-structural/proxy/LoggingProxy.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package proxy;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Logging Proxy — adds timing and audit logging around every query
|
||||
* without touching RealDatabaseConnection or any of its callers.
|
||||
*
|
||||
* This is the "cross-cutting concern" use case of Proxy,
|
||||
* the same mechanism behind Spring AOP's @Around advice.
|
||||
*/
|
||||
public class LoggingProxy implements DatabaseConnection {
|
||||
|
||||
private final DatabaseConnection target;
|
||||
|
||||
public LoggingProxy(DatabaseConnection target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {
|
||||
System.out.println("[Log] connect() at " + Instant.now());
|
||||
target.connect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeQuery(String sql) {
|
||||
long start = System.currentTimeMillis();
|
||||
System.out.println("[Log] QUERY START: " + sql);
|
||||
String result = target.executeQuery(sql);
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
System.out.println("[Log] QUERY END: " + elapsed + "ms | result: " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
System.out.println("[Log] disconnect() at " + Instant.now());
|
||||
target.disconnect();
|
||||
}
|
||||
}
|
||||
51
02-structural/proxy/Main.java
Normal file
51
02-structural/proxy/Main.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package proxy;
|
||||
|
||||
/**
|
||||
* Proxy Design Pattern — Runnable Demo
|
||||
*
|
||||
* Shows two proxy types:
|
||||
* 1. Virtual Proxy (lazy connection)
|
||||
* 2. Logging Proxy (cross-cutting concern)
|
||||
* 3. Proxy chaining (both together)
|
||||
*
|
||||
* Run: javac proxy/*.java && java proxy.Main
|
||||
* Article: https://ankurm.com/proxy-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
System.out.println("=== Proxy Design Pattern Demo ===\n");
|
||||
|
||||
// --- Virtual Proxy: lazy connection ---
|
||||
System.out.println("-- Virtual Proxy (lazy loading) --");
|
||||
DatabaseConnection lazy = new LazyConnectionProxy("jdbc:postgresql://localhost/mydb");
|
||||
lazy.connect(); // absorbed by proxy, no real connection yet
|
||||
System.out.println("(no real connection yet — saved startup time)");
|
||||
System.out.println("Result: " + lazy.executeQuery("SELECT * FROM users WHERE id=1"));
|
||||
System.out.println("Result: " + lazy.executeQuery("SELECT COUNT(*) FROM orders"));
|
||||
lazy.disconnect();
|
||||
|
||||
System.out.println();
|
||||
|
||||
// --- Logging Proxy: wraps the real connection ---
|
||||
System.out.println("-- Logging Proxy --");
|
||||
DatabaseConnection real = new RealDatabaseConnection("jdbc:mysql://localhost/shopdb");
|
||||
real.connect();
|
||||
DatabaseConnection logged = new LoggingProxy(real);
|
||||
logged.executeQuery("SELECT * FROM products LIMIT 10");
|
||||
logged.disconnect();
|
||||
|
||||
System.out.println();
|
||||
|
||||
// --- Proxy chaining: lazy + logging ---
|
||||
System.out.println("-- Chained Proxies: Lazy + Logging --");
|
||||
DatabaseConnection chain =
|
||||
new LoggingProxy(
|
||||
new LazyConnectionProxy("jdbc:oracle://localhost/warehouse"));
|
||||
chain.connect();
|
||||
chain.executeQuery("SELECT SUM(quantity) FROM inventory");
|
||||
chain.disconnect();
|
||||
|
||||
System.out.println("\n=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
33
02-structural/proxy/RealDatabaseConnection.java
Normal file
33
02-structural/proxy/RealDatabaseConnection.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package proxy;
|
||||
|
||||
/**
|
||||
* Real Subject — the actual, expensive database connection.
|
||||
* Opening it takes time. We want to delay this until truly needed.
|
||||
*/
|
||||
public class RealDatabaseConnection implements DatabaseConnection {
|
||||
|
||||
private final String url;
|
||||
|
||||
public RealDatabaseConnection(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {
|
||||
System.out.println("[Real DB] Connecting to " + url + " (expensive operation)...");
|
||||
// Simulate connection setup time
|
||||
try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
|
||||
System.out.println("[Real DB] Connected.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeQuery(String sql) {
|
||||
System.out.println("[Real DB] Executing: " + sql);
|
||||
return "ResultSet{rows=42}"; // simulated result
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
System.out.println("[Real DB] Disconnecting from " + url);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user