Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
35
03-behavioral/mediator/Main.java
Normal file
35
03-behavioral/mediator/Main.java
Normal 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 ===");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user