36 lines
1.2 KiB
Java
36 lines
1.2 KiB
Java
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 ===");
|
|
}
|
|
}
|