1. 程式人生 > >JAVA中的命令模式例項教程

JAVA中的命令模式例項教程

命令模式客戶端

現在完成一個簡單的聊天程式來完成組群聊天。

package com.journaldev.design.mediator;

public class ChatClient {

	public static void main(String[] args) {
		ChatMediator mediator = new ChatMediatorImpl();
		User user1 = new UserImpl(mediator, "Pankaj");
		User user2 = new UserImpl(mediator, "Lisa");
		User user3 = new UserImpl(mediator, "Saurabh");
		User user4 = new UserImpl(mediator, "David");
		mediator.addUser(user1);
		mediator.addUser(user2);
		mediator.addUser(user3);
		mediator.addUser(user4);

		user1.send("Hi All");

	}

}

注意,此客戶端程式非常簡單,完全沒有給出細節描述資訊是如何被處理以及調解人是否將會使用使用者物件。

上述程式的輸出如下:

Pankaj: Sending Message=Hi All
Lisa: Received Message:Hi All
Saurabh: Received Message:Hi All
David: Received Message:Hi All