Add all 23 GoF design pattern implementations

This commit is contained in:
2026-07-25 10:50:29 +05:30
commit f5688a6b32
164 changed files with 4371 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
package mediator;
/** Mediator interface — defines how colleagues communicate through the hub */
public interface ChatMediator {
void sendMessage(String message, User sender);
void addUser(User user);
}

View File

@@ -0,0 +1,27 @@
package mediator;
import java.util.ArrayList;
import java.util.List;
/**
* Concrete Mediator — the chat room that routes messages between users.
* Users talk to ChatRoom; ChatRoom talks to users. Nobody else talks to anybody.
*/
public class ChatRoom implements ChatMediator {
private final List<User> users = new ArrayList<>();
@Override
public void addUser(User user) {
users.add(user);
System.out.println(" [ChatRoom] " + user.getName() + " joined the room");
}
@Override
public void sendMessage(String message, User sender) {
for (User user : users) {
if (user != sender) { // don't echo back to sender
user.receive(message, sender.getName());
}
}
}
}

View File

@@ -0,0 +1,35 @@
package mediator;
/**
* Mediator Design Pattern — Runnable Demo
* Run: javac mediator/*.java -d out/mediator && java -cp out/mediator mediator.Main
* Article: https://ankurm.com/mediator-design-pattern-java/
*/
public class Main {
public static void main(String[] args) {
System.out.println("=== Mediator Design Pattern Demo ===\n");
ChatRoom room = new ChatRoom();
User alice = new User("Alice", room);
User bob = new User("Bob", room);
User carol = new User("Carol", room);
room.addUser(alice);
room.addUser(bob);
room.addUser(carol);
System.out.println();
alice.send("Hey everyone!");
System.out.println();
bob.send("Hi Alice and Carol!");
System.out.println();
carol.send("Good morning!");
System.out.println("\n-- Connections without Mediator: " + 3 + " users need " + (3 * 2) + " direct links --");
System.out.println("-- With Mediator: " + 3 + " users each connect only to the room --");
System.out.println("-- With N users: O(N) connections instead of O(N²) --");
System.out.println("\n=== Demo complete ===");
}
}

View File

@@ -0,0 +1,31 @@
# Mediator Design Pattern — Java Example
**Pattern:** Behavioral → Mediator
**Article:** https://ankurm.com/mediator-design-pattern-java/
## What this example shows
A chat room where users never talk to each other directly. `ChatMediator` declares the hub contract (`sendMessage()`/`addUser()`). `User` is a colleague — it knows only the mediator, never other users. `ChatRoom` is the concrete mediator: it holds the list of users and routes every message to everyone except the sender. `Main` adds three users and has each one broadcast a message, then prints the O(N) vs O(N²) connection-count comparison.
## How to run
```bash
javac mediator/*.java -d out/mediator
java -cp out/mediator mediator.Main
```
Requires Java 25.
## Post Section ↔ File Mapping
| Post Section | File(s) |
|---|---|
| Implementation: Chat Room — the Mediator interface | `ChatMediator.java` |
| Implementation: Chat Room — the Colleague | `User.java` |
| Implementation: Chat Room — the Concrete Mediator | `ChatRoom.java` |
| Implementation: Chat Room — wiring it together | `Main.java` |
Note: the "Mediator in Practice: MVC and Spring Events" snippet (`ApplicationEventPublisher`/`@EventListener` example) is illustrative only — it is not part of this repository's runnable example.
Article: https://ankurm.com/mediator-design-pattern-java/
All patterns: https://ankurm.com/design-patterns-java/

View File

@@ -0,0 +1,26 @@
package mediator;
/**
* Colleague — knows only the mediator, not other users.
* Sends messages through the mediator; receives via receive().
*/
public class User {
private final String name;
private final ChatMediator mediator;
public User(String name, ChatMediator mediator) {
this.name = name;
this.mediator = mediator;
}
public String getName() { return name; }
public void send(String message) {
System.out.println("[" + name + "] sends: " + message);
mediator.sendMessage(message, this);
}
public void receive(String message, String from) {
System.out.println(" [" + name + "] received from " + from + ": " + message);
}
}