我读到聊天是使用中介者模式的一个示例。我写了简单的聊天模仿,但我不知道在这种情况下如何使用中介模式。据我了解,我的所有对象都已经具有松散耦合,并且 Server 类类似于 Mediator。有人可以告诉我在我的样本上使用中介者模式吗?
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class Main1 {
public static void main(String[] args) {
final Server chatObserver = new Server();
chatObserver.addObserver(new Client("User1", chatObserver));
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
chatObserver.addObserver(new Client("User2", chatObserver));
}
}, 5);
t.schedule(new TimerTask() {
@Override
public void run() {
chatObserver.addObserver(new Client("User3", chatObserver));
}
}, 10);
}
static class Client extends Observable implements Observer {
private String name;
private List<Message> chatHistoryToDisplay;
private Timer timer = new Timer();
private Random random = new Random();
public Client(final String name, Observer o) {
this.name = name;
addObserver(o);
chatHistoryToDisplay = new ArrayList<Message>(((Server) o).getChatHistory());
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
int msg = random.nextInt(10);
String str = name + " writed " + msg;
System.out.println("\t"+str);
setChanged();
notifyObservers(new Message(name,String.valueOf(msg)));
}
}, 0, 10000);
}
@Override
public void update(Observable o, Object msg) {
chatHistoryToDisplay.add((Message) msg);
System.out.println("----------- "+name+" screen -----------");
for (int i=0;i<chatHistoryToDisplay.size();i++) {
Message m = chatHistoryToDisplay.get(i);
System.out.println(m.toString());
}
}
}
static class Server extends Observable implements Observer {
private List<Message> chatHistory = new ArrayList<Message>();
@Override
public void update(Observable arg0, Object msg) {
chatHistory.add((Message) msg);
setChanged();
notifyObservers(msg);
}
public Collection<Message> getChatHistory(){
return chatHistory;
}
}
static class Message {
private String author;
private String message;
public Message(String author, String message) {
this.author = author;
this.message = message;
}
@Override
public String toString() {
return author + ": " + message;
}
}
}
安慰:
User1 writed 3
----------- User1 screen -----------
User1: 3
User2 writed 9
----------- User2 screen -----------
User1: 3
User2: 9
----------- User1 screen -----------
User1: 3
User2: 9
User3 writed 9
----------- User3 screen -----------
User1: 3
User2: 9
User3: 9
----------- User2 screen -----------
User1: 3
User2: 9
User3: 9
----------- User1 screen -----------
User1: 3
User2: 9
User3: 9
等等...