📄 calculator1.java
字号:
package Jt.examples.patterns;
import Jt.*;
import Jt.xml.JtXMLHelper;
/**
* Calculator implementation based on Command and Memento.
* Creating a subclass of JtCommand and implementing the processMessage
* method is basically all that is needed. Command requests are logged via
* the inherited LogMessage method.
*/
public class Calculator1 extends JtCommand {
public static final String JtCLASS_NAME = Calculator1.class.getName();
private static final long serialVersionUID = 1L;
transient Object state = null; // Object state
private int total = 0; // Calculator Total
transient private JtMemento memento; // Contains the state of the object
// needed to implement the undo operation
public static final String ADD = "ADD";
public static final String UNDO = "UNDO";
public Calculator1 () {
}
public void setTotal (int total) {
this.total = total;
}
public int getTotal () {
return (total);
}
// save the state using JtMemento
private void saveState () {
JtMessage msg = new JtMessage (JtMemento.JtSAVE);
if (memento == null)
memento = new JtMemento ();
msg.setMsgContent(this);
sendMessage (memento, msg);
}
// restore the state using JtMemento
private void restoreState () {
JtMessage msg = new JtMessage (JtMemento.JtRESTORE);
if (memento == null) {
handleWarning ("restoreState: nothing to undo ....");
return;
}
msg.setMsgContent(this);
sendMessage (memento, msg);
}
/*
private void saveState () {
JtMessage msg = new JtMessage (JtObject.JtXML_ENCODE);
JtObject tmp = new JtObject();
msg.setMsgContent (this);
state = tmp.processMessage (msg);
if (state == null) {
handleWarning ("saveSate: Unable to save the current state"); //check
return;
}
if (memento == null)
memento = new JtMemento ();
memento.setState (state);
handleTrace ("saveState: saving state ...\n" + state);
}
*/
/*
private void restoreState () {
JtMessage msg = new JtMessage ("JtDECODE_OBJECT");
Calculator1 tmp;
JtObject copier = new JtObject ();
if (memento == null) {
handleWarning ("restoreState: nothing to undo ....");
return;
}
state = memento.getState ();
memento = null;
if (state == null) {
handleWarning ("restoreState: nothing to undo ....");
return;
}
handleTrace ("restoreState (state) ...\n" + state);
msg.setMsgContent (state);
//tmp = (Calculator1) xmlHelper.processMessage (msg);
tmp = (Calculator1) copier.processMessage (msg);
msg = new JtMessage (JtObject.JtXML_ENCODE);
msg.setMsgContent (tmp);
msg = new JtMessage ("JtCOPY");
if (tmp == null) {
handleTrace ("restoreState failed: unable to convert object from XML");
return;
}
msg.setMsgContent (tmp);
msg.setMsgData (this);
sendMessage (copier, msg);
msg = new JtMessage ("JtENCODE_OBJECT");
msg.setMsgContent (this);
}
*/
/**
* Process object messages (Command requests).
* <ul>
* <li>JtADD - Add a number (msgContent) to the total
* </ul>
*/
public Object processMessage (Object message) {
String msgid = null;
JtMessage msg = (JtMessage) message;
Object content;
if (msg == null)
return null;
// Retrieve Message ID and content
msgid = (String) msg.getMsgId ();
if (msgid == null)
return null;
content = msg.getMsgContent();
// Add a number to the total
if (msgid.equals (Calculator1.ADD)) {
saveState ();
// Log the message
logMessage (msg);
total += ((Integer) content).intValue ();
return (new Integer (total));
}
if (msgid.equals (Calculator1.UNDO)) {
restoreState ();
// Log the message
// logMessage (msg);
return (new Integer (total));
}
// JtRemove message (Remove Object)
if (msgid.equals (JtObject.JtREMOVE)) {
return (null);
}
handleError ("Calculator1.processMessage: invalid message id:" + msgid);
return (null);
}
/**
* Calculator implementation (main)
*/
public static void main(String[] args) {
JtFactory main = new JtFactory ();
JtMessage msg;
Object total;
JtKeyboard keyboard;
String input;
int num = 0;
Calculator1 calculator;
// Create calculator and Keyboard
calculator = (Calculator1) main.createObject (Calculator1.JtCLASS_NAME, "calculator");
keyboard = (JtKeyboard) main.createObject (JtKeyboard.JtCLASS_NAME, "keyboard");
System.out.println
("Enter a number to be added to the total or 'U' to undo the last operation (or <CR> to exit):");
for (;;) {
// Read input (number) from the keyboard (JtACTIVATE message)
input = (String) main.sendMessage (keyboard, new JtMessage (JtObject.JtACTIVATE));
if (input != null)
input = input.trim ();
if ("".equals (input))
break;
if ("U".equals (input) || "u".equals (input)) {
msg = new JtMessage (Calculator1.UNDO);
main.sendMessage ("calculator", msg);
System.out.println ("Total:" + main.getValue ("calculator", "total"));
continue;
}
try {
num = Integer.parseInt (input);
} catch (Exception e) {
System.err.println (e);
}
// Add the number to the Total (ADD Message)
msg = new JtMessage (Calculator1.ADD);
msg.setMsgContent (new Integer (num));
total = main.sendMessage ("calculator", msg);
System.out.println ("Total:" + total);
}
// Print the log (list of requests executed by the Calculator)
// Use JtXMLHelper to convert the log information (messageLog)
// into XML.
msg = new JtMessage (JtObject.JtXML_ENCODE);
msg.setMsgContent (main.getValue ("calculator", "messageLog"));
System.out.println ("Log:\n" +
main.sendMessage (new JtXMLHelper (), msg));
// Remove object
main.removeObject (calculator);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -