對象關係一對多轉換爲一對一的方案——中介者模式總結

前言

中介者模式,感受也沒什麼好說的,就是字面意思:對複雜對象之間進行管理,可是這個管理是雙向的,既能夠接受反饋,也能發出指令,實現對象之間聯繫的解耦合,是對象的行爲型模式。故叫作中介者,或者調停者模式。服務器

簡單的理解,就是能夠把一對多的複雜對象關係轉爲一對一的簡單關係。socket

實現代碼——簡易聊天室案例

該模式比較簡單,也是必需要結合實際的複雜業務才能說明它的做用,簡單的例子看不出來,仍是直接看代碼演示,簡單的不能再簡單的一個聊天 demoide

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(2000);
       
        while (true) {
            Socket clientSocket = serverSocket.accept();
            ClientHandler clientHandler = new ClientHandler(clientSocket);
            clientHandler.start();
        }
    }

    private static class ClientHandler extends Thread {
        private Socket clientSocket;
        private boolean flag = true;

        ClientHandler(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }

        @Override
        public void run() {
            super.run();
            System.out.println("新客戶端鏈接成功:" + clientSocket.getInetAddress() + " Port: " + clientSocket.getPort());
            try {
                OutputStream outputStream = clientSocket.getOutputStream(); 
                PrintStream outPutPrintStream = new PrintStream(outputStream);

                InputStream inputStream = clientSocket.getInputStream(); 
                BufferedReader socketInputReader = new BufferedReader(new InputStreamReader(inputStream));
                do {
                    String line = socketInputReader.readLine();
                    if ("bye".equalsIgnoreCase(line)) {
                        flag = false;
                        outPutPrintStream.println(line);
                    } else {
                        System.out.println(line);
                        outPutPrintStream.println("服務端回送:" + line.length());
                    }
                } while (flag);

                socketInputReader.close();
                outPutPrintStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

客戶端理論上 N 個,典型的1對 N 關係,服務器就是中介者(調停者),實際業務會使用 NIO 等技術,經過服務器的轉發線程,把 N 個客戶端的消息進行轉發。避免各個客戶端直接交互,減小系統的複雜度。實現對象之間聯繫的解耦合this

public class TCPClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        socket.setSoTimeout(2000); // 讀取超時時間設置
        socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(), 2000), 2000); // 鏈接本地服務器,設置鏈接超時時間 2000try {
            todo(socket);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            socket.close();
        }
    }

    private static void todo(Socket socket) throws IOException {
        InputStream inFromKeyboard = System.in; 
        BufferedReader inFromKeyboardReader = new BufferedReader(new InputStreamReader(inFromKeyboard));
        OutputStream outputStream = socket.getOutputStream();
        PrintStream outPrintStream = new PrintStream(outputStream);
        BufferedReader bufferedReader;
        boolean flag = true; 
        do {
            String line = inFromKeyboardReader.readLine();
            outPrintStream.println(line); 
            InputStream inputStream = socket.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String echo = bufferedReader.readLine();
            if ("bye".equalsIgnoreCase(echo)) { // 若是服務端發送的是 bye 就讓客戶端推出
                flag = false;
            } else {
                System.out.println(echo); // 回顯
            }
        } while (flag);

        outPrintStream.close();
        bufferedReader.close();
    }
}

中介者模式適用場景

特別適用於複雜系統之間,紛雜的對象關係管理,尤爲是對象之間引用較繁雜,結構混亂的案例,該模式特別適用於把一對多的關係轉換爲一對一的關係,使得結構更加清晰。spa

目前我的感受,相似IM 這種業務場景,使用的很是多。其它業務好像不是很突出。敬請指正。線程

中介者模式和觀察者模式

有時候,會使用觀察者模式實現中介者模式對象間的通訊。code

相關文章
相關標籤/搜索