Add per-pattern README.md for the 5 creational patterns (Factory Method, Abstract Factory, Builder, Prototype, Singleton) for consistency with all other 18 patterns

This commit is contained in:
2026-06-24 17:43:51 +05:30
parent ba4d142be2
commit 98ea39e084
5 changed files with 161 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
# Prototype Design Pattern — Java Example
**Pattern:** Creational → Prototype
**Article:** https://ankurm.com/prototype-design-pattern-java/
## What this example shows
Documents cloned from existing instances instead of rebuilt from scratch. `Copyable` declares the cloning contract. `DocumentMetadata` is a nested object that must be copied correctly for a clone to be truly independent of its source. `Document` is the concrete prototype, implementing a real (not shallow) copy. `DocumentRegistry` stores a set of ready-made prototypes that callers clone on demand instead of constructing from raw parameters. `Main` demonstrates cloning from the registry and confirms the clone and original don't share mutable state.
## How to run
```bash
javac prototype/*.java -d out/prototype
java -cp out/prototype prototype.Main
```
Requires Java 25.
## Post Section ↔ File Mapping
| 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.
Article: https://ankurm.com/prototype-design-pattern-java/
All patterns: https://ankurm.com/design-patterns-java/