An open API service indexing awesome lists of open source software.

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.

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**


image

---

## ๐Ÿ“ธ GUI Screenshot

> **Insert your GUI screenshot here**

image

---

## ๐Ÿ”ง 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?