Skip to content

Commit 3f3280a

Browse files
authored
Create README.md for mediator design pattern
1 parent 8f5a55e commit 3f3280a

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Group Chat System (Mediator Design Pattern)
2+
3+
A Spring Boot application that demonstrates the **Mediator Design Pattern**. This project simulates a chat room where users communicate through a central "Hub" (the Mediator) rather than sending messages directly to each other, reducing dependency chaos.
4+
5+
## 📖 Project Overview
6+
7+
8+
9+
**The Problem: The "Many-to-Many" Complexity**
10+
Imagine a system with 5 components that all need to talk to each other.
11+
* If User A wants to speak, they need references to User B, C, D, and E.
12+
* As the number of users grows, the connections grow exponentially (Mesh Network topology).
13+
* This creates **tight coupling**; you can't change one user without breaking others.
14+
15+
**The Solution:**
16+
The **Mediator Pattern** introduces a central interface (the Mediator) that encapsulates how a set of objects interact.
17+
* **Star Topology:** Users only know about the Mediator.
18+
* **Decoupling:** User A sends a message to the Mediator. The Mediator decides who receives it (B, C, D, etc.).
19+
20+
## 🛠️ Tech Stack
21+
22+
* **Language:** Java 17+
23+
* **Framework:** Spring Boot 3.x
24+
* **Build Tool:** Maven
25+
* **Concepts:** Behavioral Design Patterns, Decoupling, Centralized Control
26+
27+
## 📂 Project Structure
28+
29+
```text
30+
src/main/java/com/gautam/mediator/
31+
32+
├── mediator/ # The "Hub"
33+
│ ├── ChatMediator.java # Interface (Defines the contract)
34+
│ └── GroupChat.java # Concrete Logic (Routes messages)
35+
36+
├── colleague/ # The "Participants"
37+
│ ├── User.java # Abstract Class (Holds reference to Mediator)
38+
│ └── ChatUser.java # Concrete Class (Sends/Receives)
39+
40+
├── controller/
41+
│ └── ChatController.java # Simulates the chat session
42+
43+
└── MediatorApplication.java
44+
```
45+
## 🚀 How to Run
46+
47+
Prerequisites
48+
* Java Development Kit (JDK) 17 or higher
49+
* Maven
50+
51+
Steps
52+
1. Clone the repository
53+
```bash
54+
git clone https://github.com/Astrogeek77/Spring_boot_projects.git
55+
cd Spring_boot_projects
56+
```
57+
2. Build the project
58+
```bash
59+
mvn clean install
60+
```
61+
3. Run the application
62+
```bash
63+
mvn spring-boot: run
64+
```
65+
4. **Test the Endpoint** Open your browser or Postman and go to:
66+
``` http://localhost:8080/api/mediator/chat ```
67+
68+
## 📡 API Reference
69+
70+
### Run Chat Simulation
71+
Executes a pre-defined script where users join a room and exchange messages. This endpoint triggers the internal logic to demonstrate how the Mediator routes messages.
72+
73+
* **URL:** `/api/mediator/chat`
74+
* **Method:** `GET`
75+
* **Response:**
76+
```text
77+
Check Console Logs for Chat History
78+
```
79+
80+
### Console Output
81+
Check your terminal to see the routing logic in action. Notice that when "Alice" sends a message, she does **not** call `bob.receive()`. She simply calls `mediator.sendMessage()`.
82+
83+
```text
84+
-----------------------------------
85+
Alice Sending: Hello everyone! Is the meeting at 4?
86+
Bob Received: Hello everyone! Is the meeting at 4?
87+
Charlie Received: Hello everyone! Is the meeting at 4?
88+
Diana Received: Hello everyone! Is the meeting at 4?
89+
-----------------------------------
90+
Charlie Sending: Yes Alice, see you there.
91+
Alice Received: Yes Alice, see you there.
92+
Bob Received: Yes Alice, see you there.
93+
Diana Received: Yes Alice, see you there.
94+
```
95+
96+
## 🧠 Mediator Pattern Implementation Details
97+
98+
1. **The Colleague (`User.java`)**:
99+
Users hold a reference to the Mediator interface, but **not** to other users. This isolates the user classes from each other.
100+
```java
101+
protected ChatMediator mediator;
102+
103+
public void send(String msg) {
104+
// "I don't know who is in the room, I just tell the room."
105+
mediator.sendMessage(msg, this);
106+
}
107+
```
108+
109+
2. **The Mediator (`GroupChat.java`)**:
110+
The complex logic of *who gets the message* lives entirely here. This keeps the User classes clean and lightweight.
111+
```java
112+
@Override
113+
public void sendMessage(String msg, User sender) {
114+
for (User u : this.users) {
115+
// Logic: Send to everyone EXCEPT the sender
116+
if (u != sender) {
117+
u.receive(msg);
118+
}
119+
}
120+
}
121+
```
122+
123+
3. **Simplified Maintenance**:
124+
Because the communication logic is centralized, if you want to add a "Private Message" feature or a "Kick User" feature,
125+
you only modify the `GroupChat` class. You do not need to touch the `ChatUser` code or worry about breaking connections
126+
between specific users.
127+
128+
---
129+
Created for learning the Mediator Design Pattern in Java Spring Boot.

0 commit comments

Comments
 (0)