https://github.com/tharindu714/visitor-pattern-shapes-app
Compute areas cleanly โ without changing your shape classes. A lightweight, educational, and visually delightful Java Swing application demonstrating the Visitor design pattern to compute areas for multiple shape types (Circle, Square, Triangle) while keeping code open for extension.
https://github.com/tharindu714/visitor-pattern-shapes-app
java oodp visitor-pattern
Last synced: 9 months ago
JSON representation
Compute areas cleanly โ without changing your shape classes. A lightweight, educational, and visually delightful Java Swing application demonstrating the Visitor design pattern to compute areas for multiple shape types (Circle, Square, Triangle) while keeping code open for extension.
- Host: GitHub
- URL: https://github.com/tharindu714/visitor-pattern-shapes-app
- Owner: Tharindu714
- License: mit
- Created: 2025-08-12T12:43:57.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-08-12T13:34:32.000Z (10 months ago)
- Last Synced: 2025-08-12T15:30:23.373Z (10 months ago)
- Topics: java, oodp, visitor-pattern
- Language: Java
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐งฎ VisitorShapesApp โ Elegant Area Calculation with Visitor Pattern
> **Compute areas cleanly โ without changing your shape classes.**
> A lightweight, educational, and visually delightful Java Swing application demonstrating the **Visitor design pattern** to compute areas for multiple shape types (Circle, Square, Triangle) while keeping code open for extension.
---
## ๐ Highlights (Why youโll love this)
* **Pattern-first** โ teaches the Visitor pattern with real UI interactions. ๐
* **Interactive GUI** โ add shapes, compute single or total area, and observe visitor calls in the activity log. ๐ฑ๏ธ
* **Single-file, instance-based** โ everything implemented as instance inner classes, easy to read and reuse. ๐งฉ
* **Educational & production-ready** โ perfect for demos, teaching, or as a starter template for graphics/geometry apps.
---
## โจ Features
* Add circles, squares, and triangles via friendly dialogs. โ
* Compute area for a selected shape with `AreaVisitor`. โถ๏ธ
* Compute the total area of all shapes by iterating and visiting each. ฮฃ
* Activity log with timestamps and emoji badges. ๐งพ
* Clean, responsive Swing layout with custom cell rendering for shape list. ๐จ
---
## ๐ง Visitor Pattern โ Theory (Plain English)
The **Visitor design pattern** separates algorithms from the objects on which they operate. Instead of adding new behavior to many classes (which violates the Open/Closed Principle), we define a **Visitor** that implements operations for each concrete element type.
**Core participants:**
* **Element (Shape)** โ declares `accept(Visitor)`.
* **ConcreteElement (Circle, Square, Triangle)** โ implements `accept` and delegates to the visitor.
* **Visitor (AreaVisitor)** โ declares visit methods for each concrete element type and performs the operation.
**Why use Visitor here?**
* You can add new operations (e.g., perimeter, bounding box) without modifying shape classes.
* Each operation is centralized in one place (visitor), improving maintainability and extensibility.
---
## ๐ How the Visitor Pattern is applied in this project
* **`Shape`** (interface) โ declares `accept(Visitor)` and `getName()`.
* **Concrete shapes** (`Circle`, `Square`, `Triangle`) โ each implements `accept(Visitor)` by calling the corresponding `visit(this)`.
* **`Visitor`** (interface) โ declares `visit(Circle)`, `visit(Square)`, `visit(Triangle)`.
* **`AreaVisitor`** โ concrete visitor that computes and stores the last computed area. The GUI calls `accept(areaVisitor)` on shapes to compute results.
* The GUI demonstrates **double-dispatch**: `shape.accept(visitor)` โ `visitor.visit(concreteShape)` so the right method is selected at runtime without `instanceof` checks.
---
## ๐ Build & Run
**Prerequisites**
* JDK 8+ installed.
**Compile & run**
```bash
javac VisitorShapesApp.java
java VisitorShapesApp
```
If you split classes into files, compile all `.java` files in the same directory:
```bash
javac *.java
java VisitorShapesApp
```
---
## ๐งญ Usage Walkthrough (quick demo)
1. Launch the app โ youโll see a list area, control buttons, and an activity log. โ
2. Click **Add Circle / Square / Triangle** to create shapes with custom sizes. โ
3. Select a shape in the list and click **Compute Selected Area** โ the app uses `AreaVisitor` to compute the area and logs the result. โถ๏ธ
4. Click **Compute Total Area** to run the visitor across all shapes and display the sum. ฮฃ
5. Inspect the activity log to see how visitors are invoked โ itโs a great teaching aid. ๐งพ
---
## ๐ UML Diagram
> **Paste your PlantUML or image here**
---
## ๐ธ GUI Screenshot
> **Insert your GUI screenshot here**

---
## ๐ง Code Structure (conceptual)
```
VisitorShapesApp (main) -> builds UI and demonstrates visitor usage
Shape (interface) -> accept(Visitor), getName()
Circle, Square, Triangle -> concrete elements (implement accept)
Visitor (interface) -> visit(Circle), visit(Square), visit(Triangle)
AreaVisitor -> computes area (visit methods)
Dialog classes -> UI helpers to add shapes
Cell Renderer -> nicer list display
```
---
## ๐ก Extensions & Next Steps
* Add `PerimeterVisitor` or `BoundingBoxVisitor` to showcase multiple operations. ๐
* Export shapes and computed metrics to JSON/CSV. ๐ค
* Add a drawing canvas to display shapes graphically and interactively. ๐ผ๏ธ
* Integrate unit tests for Visitor logic (e.g., area formulas). ๐งช
---
## ๐งพ License & Credits
Use this as you like โ attribution appreciated. Suggestion: add an `LICENSE` file (MIT) if you plan to publish.
---
## ๐ Final Notes
This app is designed to be both an effective learning tool and a polished demo. Paste your UML and screenshot in the placeholders above, and youโll have a presentation-ready README thatโs both **professional** and **approachable**. Want me to paste a PlantUML diagram into the UML section now?