定义:
定义一个中介者,负责成员之间的复杂交互,以此减轻成员端的编程负担。
要点:
定义好中介者
实例:以一个聊天室为例,中介者负责把 每个成员的发话内容,打印在每个成员的聊天面板上
1. 定义聊天成员抽象类
public interface ChatColleague {
/** 向外部发送消息 **/
public void sendMessage( String message ) throws Exception;
/** 从外部接受消息 **/
public void getMessage( String message ) throws Exception;
/** 打印收到和要发送的消息 **/
public void printMessage() throws Exception;
/** 供外界访问消息 **/
public String accessMessage() throws Exception;
}
/**
*
* 定义所有类型成员共有的属性和方法实现
*
* @author ypqiao
*
*/
public abstract class AbstractChatColleague implements ChatColleague {
protected String id; // 聊天窗口标识
protected String name; // 聊天窗口名字
protected ChatMediator mediator; // 窗口引用的中介者
protected String message; // 当前聊天窗口所发送和接受的xiaoxi
public AbstractChatColleague( String id,String name ){
this.id = id;
this.name = name;
}
public ChatMediator getMediator() {
return mediator;
}
public void setMediator(ChatMediator mediator) {
this.mediator = mediator;
}
@Override
public void sendMessage(String message) throws Exception {
this.message = message;
mediator.actionColleague(this);
}
@Override
public void getMessage(String message) throws Exception {
this.message = message;
}
@Override
public String accessMessage() throws Exception {
return message;
}
}
**
*
* 定义某种聊天窗口类型
* 定义这个聊天窗口的打印方法
*
* @author ypqiao
*
*/
public class DefaultChatColleagueImpl extends AbstractChatColleague {
public DefaultChatColleagueImpl(String id, String name) {
super(id, name);
}
@Override
public void printMessage() throws Exception {
System.out.println("\n"+name+": "+"\n\t"+message);
}
}
2. 定义中介者行为规范
**
* 定义所有类型中介者的行为规范
*
* @author ypqiao
*
*/
public interface ChatMediator {
/** 操作一个 成员 **/
public void actionColleague( ChatColleague col ) throws Exception;
/** 添加一个成员 **/
public void addChatColleague( ChatColleague col ) throws Exception;
/** 移除一个成员 **/
public void removeChatColleague( ChatColleague col ) throws Exception;
}
3,定义中介者实现
/**
*
* 中介者默认类型
*
*
*
* @author ypqiao
*
*/
public class DefaultChatMediatorImpl implements ChatMediator {
// 成员列表
private List<ChatColleague> chatColleagues = new ArrayList<ChatColleague>();
@Override
public void actionColleague(ChatColleague col) throws Exception {
String message = col.accessMessage();
for( ChatColleague c : chatColleagues ){
c.getMessage(message);
c.printMessage();
}
}
@Override
public void addChatColleague(ChatColleague col) throws Exception {
// TODO Auto-generated method stub
chatColleagues.add(col);
}
@Override
public void removeChatColleague(ChatColleague col) throws Exception {
// TODO Auto-generated method stub
}
}
4.测试
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ChatMediator chatMediator = new DefaultChatMediatorImpl();
AbstractChatColleague c1 = new DefaultChatColleagueImpl("1","窗口1");
c1.setMediator(chatMediator);
AbstractChatColleague c2 = new DefaultChatColleagueImpl("1","窗口2");
c2.setMediator(chatMediator);
AbstractChatColleague c3 = new DefaultChatColleagueImpl("1","窗口3");
c3.setMediator(chatMediator);
chatMediator.addChatColleague(c1);
chatMediator.addChatColleague(c2);
chatMediator.addChatColleague(c3);
c1.sendMessage("小明说你好");
}
}