Skip to content

Commit 5c814ef

Browse files
authored
Create README.md for legion project (flyweight pattern)
1 parent 50b3b4d commit 5c814ef

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Legion: High-Efficiency Game Entity Manager ⚔️
2+
3+
> A memory-optimized backend service demonstrating the **Flyweight Design Pattern** to handle massive object scaling with minimal RAM footprint.
4+
5+
## 📖 Project Overview
6+
**Legion** is a backend simulation of an MMORPG game engine designed to spawn thousands of Non-Player Characters (NPCs).
7+
8+
In traditional "naive" implementations, creating 10,000 soldiers means loading 10,000 textures and sound files into memory, leading to `OutOfMemoryError` crashes. This project solves that scalability bottleneck by implementing the **Flyweight Pattern**, separating heavy, shared data from unique per-instance data.
9+
10+
## 🏗️ Design Pattern: Flyweight
11+
The core concept is to share common state between multiple objects.
12+
13+
[Image of flyweight design pattern diagram showing shared state vs unique state]
14+
15+
### Key Concepts Implemented
16+
1. **Intrinsic State (Shared):** Data that remains constant across all instances (e.g., Texture, 3D Model, Sound). Stored in the `NpcType` class.
17+
2. **Extrinsic State (Unique):** Data that changes per instance (e.g., X/Y Coordinates, Health). Stored in the `Soldier` class.
18+
3. **Flyweight Factory:** A caching mechanism (`NpcFactory`) that ensures only **one** instance of each `NpcType` is ever created in memory, regardless of how many soldiers are spawned.
19+
20+
## 🚀 Key Features
21+
* **Memory Optimization:** Reduces RAM usage drastically. Spawning 10,000 "Orcs" creates only **1** heavy `NpcType` object instead of 10,000.
22+
* **Factory Caching:** Implements a `HashMap` cache to intercept object creation requests, returning existing instances (Cache Hits) instantly.
23+
* **Scalability:** Demonstrates how game servers and graphical applications scale to handle massive entity counts.
24+
25+
## 🛠️ Tech Stack
26+
* **Core:** Java 21, Spring Boot 3.x
27+
* **Architecture:** Flyweight Design Pattern
28+
* **Build Tool:** Maven
29+
30+
## 🏃 How to Run
31+
32+
1. **Clone the repository:**
33+
```bash
34+
git clone [https://github.com/yourusername/legion.git](https://github.com/yourusername/legion.git)
35+
```
36+
2. **Run the application:**
37+
```bash
38+
mvn spring-boot:run
39+
```
40+
3. The server will start on `http://localhost:8080`.
41+
42+
## 🔌 API Endpoints (Testing Guide)
43+
44+
### 1. Spawn Army (Simulation)
45+
* **Request:** `GET /api/game/spawn`
46+
* **Result:** `Army Spawned! Check Console Logs for Memory Optimization.`
47+
* **Console Output (Proof of Optimization):**
48+
```text
49+
--- Starting Simulation ---
50+
Creation: Loading heavy assets for [Orc]... <-- Loaded ONCE (Intrinsic)
51+
Cache Hit: Returning existing [Orc] <-- Reused (Flyweight)
52+
Cache Hit: Returning existing [Orc] <-- Reused (Flyweight)
53+
Creation: Loading heavy assets for [Elf]... <-- Loaded ONCE (Intrinsic)
54+
Cache Hit: Returning existing [Elf] <-- Reused (Flyweight)
55+
--- Rendering Frame ---
56+
Rendering Orc at (0,0) using texture GreenSkin.png
57+
Rendering Orc at (10,5) using texture GreenSkin.png
58+
```
59+
## 📝 Resume Summary
60+
* **Legion Game Engine:** Implemented the **Flyweight Design Pattern** to optimize memory usage for high-volume object creation.
61+
* **Performance:** Reduced memory footprint by separating **Intrinsic State** (shared assets) from **Extrinsic State** (unique coordinates), allowing the system to scale to thousands of entities with minimal RAM overhead.
62+
63+
---
64+
*Created by Gautam Jain to demonstrate Memory Optimization Patterns in Java.*

0 commit comments

Comments
 (0)