KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtMemento


1
2
3 package Jt;
4 import java.util.*;
5 import java.lang.reflect.*;
6 import java.beans.*;
7 import java.io.*;
8
9 /**
10  * Jt implementation of the Memento pattern. This class captures
11  * the internal state of an object (originator) so that
12  * it can be restored to this state later.
13  */

14
15 public class JtMemento extends JtObject {
16
17
18   private Object JavaDoc state = null;
19
20
21 /**
22   * Specifies (saves) the originator's internal state.
23   * @param state state
24   */

25
26   public void setState (Object JavaDoc state) {
27      this.state = state;
28   }
29
30
31 /**
32   * Returns the originator's internal state.
33   */

34   public Object JavaDoc getState () {
35      return (state);
36   }
37
38   public JtMemento () {
39   }
40
41
42
43
44   /**
45     * Process object messages.
46     * <ul>
47     * <li>JtREMOVE - Performs any housekeeping that may be needed before this
48     * object is removed.
49     * </ul>
50     */

51
52   public Object JavaDoc processMessage (Object JavaDoc event) {
53
54    String JavaDoc msgid = null;
55    JtMessage e = (JtMessage) event;
56    Object JavaDoc content;
57    Object JavaDoc data;
58
59
60      if (e == null)
61     return null;
62
63      msgid = (String JavaDoc) e.getMsgId ();
64
65      if (msgid == null)
66     return null;
67
68      content = e.getMsgContent();
69      //data = e.getMsgData ();
70

71
72      if (msgid.equals ("JtREMOVE")) {
73        return (null);
74      }
75
76
77      handleError ("JtDecortator.processMessage: invalid message id:" + msgid);
78      return (null);
79
80   }
81
82  
83   /**
84    * Unit tests the messages processed by JtDecorator.
85    */

86
87
88   public static void main(String JavaDoc[] args) {
89
90     JtFactory factory = new JtFactory ();
91     JtDecorator decorator;
92  
93
94     // Create an instance of JtDecorator
95

96     decorator = (JtDecorator) factory.createObject ("Jt.JtDecorator", "decorator");
97
98     factory.removeObject ("decorator");
99
100
101   }
102
103
104 }
105
106
107
Popular Tags