Sync Factory Method, Abstract Factory, Builder, Prototype to Java 25; fix post/code mismatches

This commit is contained in:
2026-06-23 11:00:02 +05:30
parent 2f684bf3d7
commit 2fbf89875b
51 changed files with 634 additions and 337 deletions

View File

@@ -1,6 +1,6 @@
# GoF Design Patterns in Java
Complete runnable Java 17 implementations of all 23 Gang of Four design patterns.
Complete runnable Java 25 implementations of all 23 Gang of Four design patterns.
Each pattern has a dedicated article at [ankurm.com](https://ankurm.com/gof-design-patterns-java/)
with a UML diagram, step-by-step explanation, and console output.
@@ -38,7 +38,7 @@ design-patterns/
## Prerequisites
- Java 17+ (Eclipse Temurin recommended)
- Java 25+ (Eclipse Temurin recommended)
- No build tool required — plain `javac` / `java`
## Run any pattern
@@ -83,6 +83,81 @@ java -cp out\strategy strategy.Main
| Template Method | Behavioral | https://ankurm.com/template-method-design-pattern-java/ |
| Visitor | Behavioral | https://ankurm.com/visitor-design-pattern-java/ |
## Post ↔ Code Mapping
Each article is split into multiple runnable parts. This section maps every post section to the exact file(s) in this repository, so you can jump straight from "what I just read" to "the file that implements it." Updated pattern-by-pattern as each article is verified against Java 25.
### Factory Method (`01-creational/factory-method/`)
| Post Section | File(s) |
|---|---|
| The Problem: Object Creation Leaks Into Business Logic | `NotificationService.java` (anti-pattern shown for contrast — not called by `Main`) |
| Part 1 — The Notification Interface | `Notification.java` |
| Part 2 — The Concrete Notification Classes | `EmailNotification.java`, `SmsNotification.java`, `PushNotification.java` |
| Part 3 — The NotificationSender Class | `NotificationSender.java` |
| Part 4 — The Concrete Sender Subclasses | `EmailSender.java`, `SmsSender.java`, `PushSender.java` |
| Part 5 — Running It: Client Code and Output | `Main.java` |
| Adding a New Channel (Slack) | `SlackNotification.java`, `SlackSender.java` |
Run it:
```bash
javac 01-creational/factory-method/*.java -d out/factory-method
java -cp out/factory-method factorymethod.Main
```
### Abstract Factory (`01-creational/abstract-factory/`)
| Post Section | File(s) |
|---|---|
| Part 1 — The Abstract Products: Button and TextField | `Button.java`, `TextField.java` |
| Part 2 — The Concrete Products (Light family) | `LightButton.java`, `LightTextField.java` |
| Part 2 — The Concrete Products (Dark family) | `DarkButton.java`, `DarkTextField.java` |
| Part 3 — The Abstract Factory: UIFactory Interface | `UIFactory.java` |
| Part 4 — The Concrete Factories | `LightThemeFactory.java`, `DarkThemeFactory.java` |
| Part 5 — The Client: Application | `Application.java`, `Main.java` |
Run it:
```bash
javac 01-creational/abstract-factory/*.java -d out/abstract-factory
java -cp out/abstract-factory abstractfactory.Main
# or, for the dark theme family:
java -Dapp.theme=dark -cp out/abstract-factory abstractfactory.Main
```
### Builder (`01-creational/builder/`)
| Post Section | File(s) |
|---|---|
| Part 1 — The Product: HttpRequest (Immutable) | `HttpRequest.java` |
| Part 2 — The Director: Encapsulating Common Configurations | `HttpClientConfig.java` |
| Part 3 — Using the Builder: Client Code | `Main.java` |
Note: the Lombok `@Builder` example and the JDK standard-library snippets (`HttpRequest.newBuilder()`, `StringBuilder`, `ProcessBuilder`) are illustrative only — they are not part of this repository's runnable example.
Run it:
```bash
javac 01-creational/builder/*.java -d out/builder
java -cp out/builder builder.Main
```
### Prototype (`01-creational/prototype/`)
| Post Section | File(s) |
|---|---|
| Part 1 — The Prototype Interface: Copyable | `Copyable.java` |
| Part 2 — The Nested Object: DocumentMetadata | `DocumentMetadata.java` |
| Part 3 — The Concrete Prototype: Document | `Document.java` |
| Part 4 — The Prototype Registry: DocumentRegistry | `DocumentRegistry.java` |
| Part 5 — Using the Prototype: Client Code | `Main.java` |
Note: the `ShallowVsDeepDemo` snippet (shallow vs. deep copy) and the `Cloneable`-based example are illustrative only — they are not part of this repository's runnable example.
Run it:
```bash
javac 01-creational/prototype/*.java -d out/prototype
java -cp out/prototype prototype.Main
```
## Reference
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides