1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import java.util.Date;
class User { String name;
public String getName() { return name; }
public User(String name) { this.name = name; }
public void sendMessage(String message) { ChatRoom.showMessage(this, message); } }
class ChatRoom { public static void showMessage(User user, String message) { System.out.println(new Date() + " [" + user.getName() + "] : " + message); } }
public class MediatorPatternDemo { public static void main(String[] args) { User robert = new User("Robert"); User john = new User("John");
robert.sendMessage("Hi! John!"); john.sendMessage("Hello! Robert!"); } }
|