1

我读到聊天是使用中介者模式的一个示例。我写了简单的聊天模仿,但我不知道在这种情况下如何使用中介模式。据我了解,我的所有对象都已经具有松散耦合,并且 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

等等...

4

1 回答 1

2

不确定,正确理解你的问题。

但我认为,在这种情况下,你实际上并不需要mediator模式。如果有很多不同的对象必须交换一些数据就好了。mediator 例如,ChatClient1 实现、 ChatClient2实现等。它们必须在mediator. Mediator 对其他注册类一无所知,但对其他类ChatClient一无所知。大多数时候,这种模式用于 UI。在您的情况下,由于您只有一种类型的 ChatClient 和一种类型的服务器,observer因此是合适的

于 2012-02-13T21:21:22.080 回答