|
| 1 | +# Smart Order Processing Engine (SOPE) 🚀 |
| 2 | + |
| 3 | +> A robust, event-driven Spring Boot backend demonstrating advanced design patterns and production-ready architecture. |
| 4 | +
|
| 5 | +## 📋 Project Overview |
| 6 | +This project is a backend simulation of an e-commerce order processing system. It is designed specifically to demonstrate **clean architecture**, **SOLID principles**, and the practical application of **GoF Design Patterns** in a modern Spring Boot ecosystem. |
| 7 | + |
| 8 | +Unlike simple CRUD applications, SOPE handles complex business logic like dynamic pricing, state transitions, and asynchronous notifications without tight coupling. |
| 9 | + |
| 10 | +## 🏗️ Architecture & Design Patterns |
| 11 | +This project moves beyond "Hello World" by implementing specific patterns to solve real architectural problems: |
| 12 | + |
| 13 | +| Pattern | Component | Problem Solved | |
| 14 | +| :--- | :--- | :--- | |
| 15 | +| **Strategy Pattern** | Payment Service | Allows switching between payment methods (Credit Card, PayPal, BitCoin) at runtime without modifying core logic. Adheres to the **Open/Closed Principle**. | |
| 16 | +| **State Pattern** | Order Lifecycle | Manages complex state transitions (`New` -> `Paid` -> `Shipped`) polymorphically, eliminating massive `if-else` blocks and ensuring valid transition rules. | |
| 17 | +| **Decorator Pattern** | Pricing Engine | Enables dynamic calculation of order totals by "wrapping" the base price with tax, shipping, or packaging fees in any combination. | |
| 18 | +| **Observer Pattern** | Notification System | Uses Spring Events (`ApplicationEventPublisher`) to decouple the Order Service from Email/Inventory services. Allows for **Asynchronous** processing. | |
| 19 | +| **Factory (Spring IoC)** | Payment Factory | Leverages Spring's Dependency Injection to automatically detect and inject the correct strategies at runtime. | |
| 20 | + |
| 21 | +## 🛠️ Tech Stack |
| 22 | +* **Core:** Java 21, Spring Boot 3.x |
| 23 | +* **Data:** Spring Data JPA, H2 Database (In-Memory for demo) |
| 24 | +* **Testing:** JUnit 5, Mockito |
| 25 | +* **Tools:** Maven, Lombok (Optional), Postman |
| 26 | + |
| 27 | +## 🌟 Key Features |
| 28 | +* **Asynchronous Event Handling:** Uses `@Async` to handle non-blocking tasks (like email simulation) to improve API response times. |
| 29 | +* **Global Exception Handling:** Centralized error management using `@ControllerAdvice` to return standardized JSON error responses (e.g., `404 Not Found` vs `500 Internal Error`). |
| 30 | +* **Extensible Design:** New payment methods or pricing rules can be added by creating new classes, not touching existing code. |
| 31 | + |
| 32 | +## 🚀 How to Run |
| 33 | +1. **Clone the repository:** |
| 34 | + ```bash |
| 35 | + git clone [https://github.com/yourusername/smart-order-engine.git](https://github.com/yourusername/smart-order-engine.git) |
| 36 | + ``` |
| 37 | +2. **Build the project:** |
| 38 | + ```bash |
| 39 | + mvn clean install |
| 40 | + ``` |
| 41 | +3. **Run the application:** |
| 42 | + ```bash |
| 43 | + mvn spring-boot:run |
| 44 | + ``` |
| 45 | +4. The server will start on `http://localhost:8080`. |
| 46 | + |
| 47 | +## 🔌 API Endpoints (Testing Guide) |
| 48 | + |
| 49 | +### 1. Payment (Strategy Pattern) |
| 50 | +* **POST** `/api/orders/checkout?type=CREDIT_CARD&amount=100` |
| 51 | +* *Response:* "Payment processed via CREDIT_CARD" |
| 52 | + |
| 53 | +### 2. Order Lifecycle (State Pattern & Async Events) |
| 54 | +* **POST** `/api/orders/create` |
| 55 | + * *Creates a new order. Watch console logs for Async Email/Inventory events.* |
| 56 | +* **POST** `/api/orders/{id}/next` |
| 57 | + * *Moves order state forward (New -> Paid -> Shipped).* |
| 58 | + |
| 59 | +### 3. Pricing Engine (Decorator Pattern) |
| 60 | +* **GET** `/api/orders/price?cost=100&tax=true&pack=true` |
| 61 | + * *Calculates total cost with Tax (+10%) and Packaging (+$5).* |
| 62 | + |
| 63 | +## 🧪 Testing |
| 64 | +Run the comprehensive test suite including Unit Tests for logic and Integration Tests for persistence: |
| 65 | +```bash |
| 66 | +mvn test |
| 67 | +``` |
| 68 | +--- |
| 69 | +Created by Gautam Jain as a demonstration of advanced Spring Boot architecture. |
0 commit comments