Proxy: sync post code blocks to repo exactly, Java 25, add H6 file labels and README mapping
This commit is contained in:
32
02-structural/proxy/README.md
Normal file
32
02-structural/proxy/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# Proxy Design Pattern — Java Example
|
||||
|
||||
**Pattern:** Structural → Proxy
|
||||
**Article:** https://ankurm.com/proxy-design-pattern-java/
|
||||
|
||||
## What this example shows
|
||||
|
||||
A `DatabaseConnection` interface is implemented by a real, expensive connection (`RealDatabaseConnection`) and by two proxies that sit in front of it: `LazyConnectionProxy` defers opening the real connection until the first query actually runs (virtual proxy), and `LoggingProxy` wraps any `DatabaseConnection` — real or proxied — and adds timing/audit logging around every call (cross-cutting concern proxy). Because both proxies implement the same interface as the real subject, they compose: `Main` chains `LoggingProxy` around a `LazyConnectionProxy` to get lazy loading and logging together.
|
||||
|
||||
## How to run
|
||||
|
||||
```bash
|
||||
javac proxy/*.java -d out/proxy
|
||||
java -cp out/proxy proxy.Main
|
||||
```
|
||||
|
||||
Requires Java 25.
|
||||
|
||||
## Post Section ↔ File Mapping
|
||||
|
||||
| Post Section | File(s) |
|
||||
|---|---|
|
||||
| The Subject Interface | `DatabaseConnection.java` |
|
||||
| The Real Subject | `RealDatabaseConnection.java` |
|
||||
| Virtual Proxy: Lazy Initialisation | `LazyConnectionProxy.java` |
|
||||
| Logging Proxy: Cross-Cutting Concerns | `LoggingProxy.java` |
|
||||
| Wiring It Together: Proxy Chaining | `Main.java` |
|
||||
|
||||
Note: the "Dynamic Proxy with java.lang.reflect.Proxy" snippet is illustrative only — it is not part of this repository's runnable example.
|
||||
|
||||
Article: https://ankurm.com/proxy-design-pattern-java/
|
||||
All patterns: https://ankurm.com/design-patterns-java/
|
||||
18
README.md
18
README.md
@@ -287,6 +287,24 @@ javac 02-structural/flyweight/*.java -d out/flyweight
|
||||
java -cp out/flyweight flyweight.Main
|
||||
```
|
||||
|
||||
### Proxy (`02-structural/proxy/`)
|
||||
|
||||
| Post Section | File(s) |
|
||||
|---|---|
|
||||
| The Subject Interface | `DatabaseConnection.java` |
|
||||
| The Real Subject | `RealDatabaseConnection.java` |
|
||||
| Virtual Proxy: Lazy Initialisation | `LazyConnectionProxy.java` |
|
||||
| Logging Proxy: Cross-Cutting Concerns | `LoggingProxy.java` |
|
||||
| Wiring It Together: Proxy Chaining | `Main.java` |
|
||||
|
||||
Note: the "Dynamic Proxy with java.lang.reflect.Proxy" snippet is illustrative only — it is not part of this repository's runnable example.
|
||||
|
||||
Run it:
|
||||
```bash
|
||||
javac 02-structural/proxy/*.java -d out/proxy
|
||||
java -cp out/proxy proxy.Main
|
||||
```
|
||||
|
||||
## Reference
|
||||
|
||||
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
||||
|
||||
Reference in New Issue
Block a user