Visitor: sync post code blocks to repo exactly (post used a completely different design - return-value visitors vs repo's void/print-in-place visitors, different Triangle model), split merged Circle/Rectangle/Triangle and AreaCalculator/PerimeterCalculator blocks, Java 25, H6 file labels, fix stale console output, README mapping
This commit is contained in:
33
03-behavioral/visitor/README.md
Normal file
33
03-behavioral/visitor/README.md
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# Visitor Design Pattern — Java Example
|
||||||
|
|
||||||
|
**Pattern:** Behavioral → Visitor
|
||||||
|
**Article:** https://ankurm.com/visitor-design-pattern-java/
|
||||||
|
|
||||||
|
## What this example shows
|
||||||
|
|
||||||
|
A shape hierarchy that never changes, with operations added as separate visitor classes instead of methods on each shape. `ShapeVisitor` declares one `visit()` overload per concrete type. `Shape` declares `accept(ShapeVisitor)` — the double-dispatch trick: the shape hands itself to the visitor as a concretely-typed `this`, so overload resolution picks the right `visit()` method with no `instanceof` checks anywhere. `Circle`, `Rectangle`, and `Triangle` are plain data holders that know nothing about area or perimeter. `AreaCalculator` and `PerimeterCalculator` are two concrete visitors — adding the second one required zero changes to any shape class. `Main` runs both visitors across the same three shapes.
|
||||||
|
|
||||||
|
## How to run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
javac visitor/*.java -d out/visitor
|
||||||
|
java -cp out/visitor visitor.Main
|
||||||
|
```
|
||||||
|
|
||||||
|
Requires Java 25.
|
||||||
|
|
||||||
|
## Post Section ↔ File Mapping
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| Implementation: Shape Geometry Operations — the Visitor interface | `ShapeVisitor.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — the Element interface | `Shape.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — Circle | `Circle.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — Rectangle | `Rectangle.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — Triangle | `Triangle.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — AreaCalculator | `AreaCalculator.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — PerimeterCalculator | `PerimeterCalculator.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — wiring it together | `Main.java` |
|
||||||
|
|
||||||
|
Article: https://ankurm.com/visitor-design-pattern-java/
|
||||||
|
All patterns: https://ankurm.com/design-patterns-java/
|
||||||
19
README.md
19
README.md
@@ -460,6 +460,25 @@ javac 03-behavioral/template-method/*.java -d out/template-method
|
|||||||
java -cp out/template-method template.Main
|
java -cp out/template-method template.Main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Visitor (`03-behavioral/visitor/`)
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| Implementation: Shape Geometry Operations — the Visitor interface | `ShapeVisitor.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — the Element interface | `Shape.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — Circle | `Circle.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — Rectangle | `Rectangle.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — Triangle | `Triangle.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — AreaCalculator | `AreaCalculator.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — PerimeterCalculator | `PerimeterCalculator.java` |
|
||||||
|
| Implementation: Shape Geometry Operations — wiring it together | `Main.java` |
|
||||||
|
|
||||||
|
Run it:
|
||||||
|
```bash
|
||||||
|
javac 03-behavioral/visitor/*.java -d out/visitor
|
||||||
|
java -cp out/visitor visitor.Main
|
||||||
|
```
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
|
|
||||||
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
||||||
|
|||||||
Reference in New Issue
Block a user