From b76a0d1b69bcd470c7358ce3d3409c8baf41679b Mon Sep 17 00:00:00 2001 From: Ankur Date: Wed, 24 Jun 2026 17:36:10 +0530 Subject: [PATCH] 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 --- 03-behavioral/visitor/README.md | 33 +++++++++++++++++++++++++++++++++ README.md | 19 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 03-behavioral/visitor/README.md diff --git a/03-behavioral/visitor/README.md b/03-behavioral/visitor/README.md new file mode 100644 index 0000000..6bd2c40 --- /dev/null +++ b/03-behavioral/visitor/README.md @@ -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/ diff --git a/README.md b/README.md index 4bb012d..4866088 100644 --- a/README.md +++ b/README.md @@ -460,6 +460,25 @@ javac 03-behavioral/template-method/*.java -d out/template-method 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 - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides