KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtFlyweight


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 Flyweight pattern.
11  */

12
13 public class JtFlyweight extends JtComposite {
14
15   private Object JavaDoc factory = null;
16
17
18   public JtFlyweight () {
19   }
20
21
22
23 /**
24   * Specifies the factory.
25   *
26   * @param factory factory
27   */

28
29   public void setFactory (Object JavaDoc factory) {
30      this.factory = factory;
31
32   }
33
34 /**
35   * Returns the factory.
36   */

37
38   public Object JavaDoc getFactory () {
39      return (factory);
40   }
41
42
43
44
45   /**
46     * Process object messages.
47     * <ul>
48     * <li> JtGET_FLYWEIGHT- Returns the flyweight specified by msgData if it exists.
49     * If it doesn't exist, create it and return it.
50     * </ul>
51     * @param message Jt Message
52     */

53
54
55   public Object JavaDoc processMessage (Object JavaDoc message) {
56
57    String JavaDoc msgid = null;
58    JtMessage e = (JtMessage) message;
59    Object JavaDoc content;
60    Object JavaDoc data;
61    JtMessage tmp;
62    JtInterface aux, aux1;
63    
64
65
66      if (e == null)
67     return null;
68
69      msgid = (String JavaDoc) e.getMsgId ();
70
71      if (msgid == null)
72     return null;
73
74      content = e.getMsgContent();
75      data = e.getMsgData ();
76
77      // Remove this object
78
if (msgid.equals ("JtREMOVE")) {
79        return (this);
80      }
81
82
83      if (msgid.equals ("JtGET_FLYWEIGHT")) {
84        tmp = new JtMessage ("JtGET_CHILD");
85        tmp.setMsgData (e.getMsgData ());
86        aux = (JtInterface) super.processMessage (tmp);
87
88        if (aux != null)
89          return (aux);
90
91              
92        if (factory == null) {
93          handleError ("processMessage: factory attribute needs to be set");
94          return (null);
95        }
96
97        //handleTrace ("Jt.Flyweight: processMessage creating a new flyweight");
98

99        tmp = new JtMessage ("JtCREATE_FLYWEIGHT");
100        tmp.setMsgData (e.getMsgData ());
101
102        aux1 = (JtInterface) sendMessage (factory, tmp);
103
104        tmp = new JtMessage ("JtADD_CHILD");
105        tmp.setMsgContent (aux1);
106        tmp.setMsgData (e.getMsgData ());
107        super.processMessage (tmp);
108        return (aux1);
109
110
111      }
112
113       
114      return (super.processMessage (message));
115      
116       
117      //handleError ("JtMediator.processMessage: invalid message id:" + msgid);
118
//return (null);
119

120   }
121
122  
123   /**
124    * Unit tests the messages processed by JtFlyweight.
125    */

126
127   public static void main(String JavaDoc[] args) {
128
129     JtObject main = new JtFactory ();
130
131     JtFlyweight flyweightp;
132
133     // Create an instance of JtColletion
134

135     flyweightp = (JtFlyweight) main.createObject ("Jt.JtFlyweight", "flyweight");
136
137     //main.sendMessage (composite, new JtMessage ("JtTEST"));
138

139
140     main.removeObject ("flyweight");
141
142
143   }
144
145 }
146
147
148
Popular Tags