📄 mediator.java
字号:
package Jt.examples.patterns;
import Jt.*;
/**
* Chat room implementation based on the Mediator pattern. Colleagues run in separate
* threads.
*/
public class Mediator extends JtMediator {
public static final String JtCLASS_NAME = Mediator.class.getName();
private static final long serialVersionUID = 1L;
public static final String JOIN = "JOIN";
public static final String MESSAGE = "MESSAGE";
public static final String EXIT = "EXIT";
public Mediator () {
}
// Broadcast a message to all the members
private void broadcastMessage (JtMessage msg) {
JtMessage tmp;
if (msg == null)
return;
tmp = new JtMessage (JtObject.JtBROADCAST);
tmp.setMsgContent (msg);
processMessage (tmp);
}
/**
* Process object messages.
* <ul>
* </ul>
* @param message Jt Message
*/
public Object processMessage (Object message) {
String msgid = null;
JtMessage e = (JtMessage) message;
Object content;
JtMessage tmp;
JtObject colleague;
if (e == null)
return null;
msgid = (String) e.getMsgId ();
if (msgid == null)
return null;
content = e.getMsgContent();
//data = e.getMsgData ();
// Remove this object
if (msgid.equals (JtObject.JtREMOVE)) {
return (null);
}
// Join the chat room
if (msgid.equals (Mediator.JOIN)) {
colleague = (JtObject) e.getMsgContent ();
System.out.println (colleague.getObjName() + " has joined");
tmp = new JtMessage (Mediator.MESSAGE);
tmp.setMsgContent (colleague.getObjName() + " has joined");
// Alert everyone about the new member
broadcastMessage (tmp);
// Add the new member to the chat room
tmp = new JtMessage (JtComposite.JtADD_CHILD);
tmp.setMsgContent (e.getMsgContent ());
//tmp.setMsgData (colleague.getObjName());
processMessage (tmp);
return (this);
}
// Send a message to the chat room (everyone)
if (msgid.equals (Mediator.MESSAGE)) {
colleague = (JtObject) e.getMsgData ();
broadcastMessage (e);
System.out.println (colleague.getObjName() + ":" + content);
return (this);
}
// Exit the chat room
if (msgid.equals (Mediator.EXIT)) {
colleague = (JtObject) e.getMsgContent ();
tmp = new JtMessage (Mediator.MESSAGE);
tmp.setMsgContent (colleague.getObjName() + " is exiting ...");
broadcastMessage (tmp);
System.out.println (colleague.getObjName() + " is exiting ...");
tmp = new JtMessage (JtComposite.JtREMOVE_CHILD);
tmp.setMsgData (e.getMsgFrom ());
return (this);
}
// Let the superclass handle all the other messages
return (super.processMessage (message));
}
static private char waitForInputKey () {
char c = ' ';
try {
c = (char) System.in.read ();
while (System.in.available () > 0)
System.in.read ();
} catch (Exception e) {
e.printStackTrace ();
}
return (c);
}
// Test program
public static void main(String[] args) {
JtObject factory = new JtFactory ();
JtMediator mediator;
Colleague colleague1, colleague2, colleague3;
System.out.println ("Press any key to start/stop the chat room demo ...");
waitForInputKey ();
// Create an instance of the chat room
mediator = (JtMediator) factory.createObject (Mediator.JtCLASS_NAME, "mediator");
colleague1 = (Colleague) factory.createObject (Colleague.JtCLASS_NAME, "Jenny");
factory.setValue (colleague1, "greetingMessage", "Hi folks! How are you all doing ?");
// Activate the first member
factory.setValue (colleague1, "mediator", mediator);
factory.sendMessage (colleague1, new JtMessage (JtObject.JtACTIVATE));
// Activate the second member
colleague2 = (Colleague) factory.createObject (Colleague.JtCLASS_NAME, "Daniel");
factory.setValue (colleague2, "mediator", mediator);
factory.sendMessage (colleague2, new JtMessage (JtObject.JtACTIVATE));
// Activate the third member
colleague3 = (Colleague) factory.createObject (Colleague.JtCLASS_NAME, "Mary");
factory.setValue (colleague3, "mediator", mediator);
factory.sendMessage (colleague3, new JtMessage (JtObject.JtACTIVATE));
waitForInputKey ();
// Remove the objects
factory.removeObject ("mediator");
factory.removeObject ("Jenny");
factory.removeObject ("Daniel");
factory.removeObject ("Mary");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -