jtcommand.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 150 行
JAVA
150 行
package Jt;
/**
* Jt Implementation of the Command pattern. This object inherits from JtThread.
* It can execute using a separate/independent thread and process messages (requests) asynchronously
* via a message queue.
*/
public class JtCommand extends JtThread {
public static final String JtCLASS_NAME = JtCommand.class.getName();
private static final long serialVersionUID = 1L;
private JtList messageLog = new JtList ();
private boolean synchronous = true;
public JtCommand() {
}
/**
* Verifies if this object should process messages synchronously or asynchronously
* via a message queue and a separate thread.
*/
public boolean getSynchronous () {
return (synchronous);
}
/**
* Specifies synchronous/asynchronous mode of processing messages.
*/
public void setSynchronous (boolean synchronous) {
this.synchronous = synchronous;
}
/**
* Returns the message (request) log. This is basically the list of messages processed
* by this object.
*/
public JtList getMessageLog () {
return (messageLog);
}
/**
* Changes the message (request) log.
*/
public void setMessageLog (JtList messageLog) {
this.messageLog = messageLog;
}
/**
* Enqueues a message (request) if asynchronous mode is set. Otherwise process the message
* by calling processMesssage directly. sendMessage () calls this function when
* the target class is a subclass of JtThread.
*/
synchronized public Object enqueueMessage (Object msg) // chec
{
if (synchronous)
return (processMessage (msg));
else
return (super.enqueueMessage (msg));
}
/**
* Log a message (request).
*/
protected void logMessage (Object msg)
{
JtMessage m = new JtMessage (JtList.JtADD);
if (msg == null)
return;
m.setMsgContent (msg);
messageLog.processMessage (m);
}
/**
* Process object messages. Invokes the superclass to process JtREMOVE, JtSTART and
* JtSTOP.
*/
public Object processMessage (Object event) {
String msgid = null;
JtMessage e = (JtMessage) event;
if (e == null)
return null;
msgid = (String) e.getMsgId ();
if (msgid == null)
return null;
//if (msgid.equals ("JtREMOVE") ||
// msgid.equals ("JtSTART") || msgid.equals ("JtSTOP")) {
// return (super.processMessage (event));
//}
return (super.processMessage(event));
}
/**
* Demonstrates the messages processed by JtCommand
*/
public static void main(String[] args) {
JtFactory main = new JtFactory ();
JtCommand command;
// Create an instance of JtCommand
command = (JtCommand) main.createObject (JtCommand.JtCLASS_NAME, "command");
main.sendMessage (command, new JtMessage (JtThread.JtSTART));
main.removeObject ("command");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?