Skip to content

Commit 50b3b4d

Browse files
committed
added legion project for flyweight design pattern
2 parents c0f0e3c + 5e9d8ad commit 50b3b4d

38 files changed

Lines changed: 1595 additions & 35 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Getting Started
2+
3+
### Reference Documentation
4+
For further reference, please consider the following sections:
5+
6+
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
7+
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/4.0.1/maven-plugin)
8+
* [Create an OCI image](https://docs.spring.io/spring-boot/4.0.1/maven-plugin/build-image.html)
9+
* [Spring Web](https://docs.spring.io/spring-boot/4.0.1/reference/web/servlet.html)
10+
11+
### Guides
12+
The following guides illustrate how to use some features concretely:
13+
14+
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
15+
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
16+
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/)
17+
18+
### Maven Parent overrides
19+
20+
Due to Maven's design, elements are inherited from the parent POM to the project POM.
21+
While most of the inheritance is fine, it also inherits unwanted elements like `<license>` and `<developers>` from the parent.
22+
To prevent this, the project POM contains empty overrides for these elements.
23+
If you manually switch to a different parent and actually want the inheritance, you need to remove those overrides.
24+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Sentinel: Chain of Responsibility Risk Engine 🛡️
2+
3+
> A modular banking transaction validation system demonstrating the **Chain of Responsibility** design pattern to achieve loose coupling and fail-fast processing.
4+
5+
## 📖 Project Overview
6+
**Sentinel** is a financial middleware service designed to validate transactions through a dynamic pipeline of security checks.
7+
8+
Instead of a monolithic `validateTransaction()` method with complex nested `if-else` logic, this project utilizes the **Chain of Responsibility** pattern. Each validation step (Sanity, Blacklist, Fraud) is an independent component that can be added, removed, or reordered without modifying the core service logic.
9+
10+
## 🏗️ Design Pattern: Chain of Responsibility
11+
The application routes a request through a chain of handlers. If any handler fails the request, the process stops immediately (Fail-Fast).
12+
13+
```mermaid
14+
graph LR
15+
A[Request] --> B(Sanity Check)
16+
B -- Pass --> C(Blacklist Check)
17+
C -- Pass --> D(Fraud Check)
18+
D -- Pass --> E[Approved ✅]
19+
B -- Fail --> F[Rejected ❌]
20+
C -- Fail --> F
21+
D -- Fail --> F
22+
```
23+
## 🗝️ Key Components
24+
1. **`RiskHandler` (Abstract Handler):** Defines the contract and manages the link to the `next` handler in the chain.
25+
2. **`SanityCheckHandler`:** The first line of defense; validates basic constraints (e.g., Amount must be > 0).
26+
3. **`BlacklistHandler`:** Simulates a security check against a database of banned users.
27+
4. **`LargeTransactionHandler`:** Applies business logic to flag or reject transactions exceeding specific financial thresholds.
28+
5. **`TransactionService`:** Orchestrates the chain creation using Spring's Dependency Injection (`@PostConstruct`).
29+
30+
## 🛠️ Tech Stack
31+
* **Core:** Java 21, Spring Boot 3.x
32+
* **Architecture:** Chain of Responsibility Design Pattern
33+
* **Build Tool:** Maven
34+
* **Testing:** Postman / cURL
35+
36+
## 🏃 How to Run
37+
38+
1. **Clone the repository:**
39+
```bash
40+
git clone [https://github.com/yourusername/sentinel.git](https://github.com/yourusername/sentinel.git)
41+
```
42+
2. **Navigate to the project directory:**
43+
```bash
44+
cd sentinel
45+
```
46+
3. **Run the application:**
47+
```bash
48+
mvn spring-boot:run
49+
```
50+
4. The server will start on `http://localhost:8080`.
51+
52+
## 🔌 API Endpoints (Testing Guide)
53+
54+
### 1. Successful Transaction (Happy Path)
55+
* **Request:** `POST /api/transactions/pay?user=John&amount=500`
56+
* **Result:** `Transaction Approved!`
57+
* **Console Logs:**
58+
```text
59+
✅ Sanity Check Passed
60+
✅ Blacklist Check Passed
61+
✅ Limit Check Passed
62+
```
63+
64+
### 2. Validation Failure (Fail-Fast Demo)
65+
* **Request:** `POST /api/transactions/pay?user=John&amount=-100`
66+
* **Result:** `Transaction Rejected.`
67+
* **Console Logs:**
68+
```text
69+
❌ Validation Failed: Amount must be positive.
70+
(Processing stopped here. Blacklist/Limit checks were NEVER executed)
71+
```
72+
73+
### 3. Security Failure (Blacklist Demo)
74+
* **Request:** `POST /api/transactions/pay?user=BANNED_USER&amount=500`
75+
* **Result:** `Transaction Rejected.`
76+
* **Console Logs:**
77+
```text
78+
✅ Sanity Check Passed
79+
❌ Validation Failed: Account is blacklisted.
80+
```
81+
---
82+
Created by Gautam Jain to demonstrate Design Pattern implementation in Spring Boot.

0 commit comments

Comments
 (0)