31 lines
1.0 KiB
Markdown
31 lines
1.0 KiB
Markdown
# Composite Design Pattern — Java Example
|
|
|
|
**Pattern:** Structural → Composite
|
|
**Article:** https://ankurm.com/composite-design-pattern-java/
|
|
|
|
## What this example shows
|
|
|
|
Builds a file system tree where files (leaves) and directories (composites) share the same `FileSystemItem` interface. Callers compute size or print the tree without ever checking whether a node is a file or a directory.
|
|
|
|
## How to run
|
|
|
|
```bash
|
|
javac composite/*.java -d out/composite
|
|
java -cp out/composite composite.Main
|
|
```
|
|
|
|
Requires Java 25.
|
|
|
|
## Post Section ↔ File Mapping
|
|
|
|
| Post Section | File(s) |
|
|
|---|---|
|
|
| The Problem: Treating Leaves and Containers Uniformly | illustrative only — not part of this repository's runnable example |
|
|
| Step 1 — The Component Interface | `FileSystemItem.java` |
|
|
| Step 2 — The Leaf (File) | `File.java` |
|
|
| Step 3 — The Composite (Directory) | `Directory.java` |
|
|
| Building and Using the Tree | `Main.java` |
|
|
|
|
Article: https://ankurm.com/composite-design-pattern-java/
|
|
All patterns: https://ankurm.com/design-patterns-java/
|