KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > patterns > SwitchFactory


1 package Jt.examples.patterns;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6 import Jt.*;
7
8
9 /**
10  * Demonstrates the use of the Abstract Factory pattern (see Flyweight.java).
11  */

12
13
14 public class SwitchFactory extends JtAbstractFactory {
15
16
17
18   public SwitchFactory() {
19   }
20
21
22
23
24   /**
25     * Process object messages.
26     * @param event Jt Message
27     */

28
29   public Object JavaDoc processMessage (Object JavaDoc event) {
30
31    String JavaDoc msgid = null;
32    JtMessage e = (JtMessage) event;
33    Object JavaDoc content;
34    String JavaDoc data;
35    Object JavaDoc output;
36
37  
38      if (e == null)
39     return null;
40
41      msgid = (String JavaDoc) e.getMsgId ();
42
43      if (msgid == null)
44     return null;
45
46      content = e.getMsgContent();
47
48
49      // Remove this object
50
if (msgid.equals ("JtREMOVE")) {
51        return (this);
52      }
53
54      if (msgid.equals ("JtCREATE_FLYWEIGHT")) {
55        data = (String JavaDoc) e.getMsgData ();
56        if (data == null) {
57          handleError ("JtCREATE_FLYWEIGHT: invalid key");
58          return (null);
59        }
60
61        if (data.equals ("On"))
62          output = new OnSwitch ();
63        else
64          output = new OffSwitch ();
65        
66        return (output);
67      }
68
69      handleError ("processMessage: invalid message id:" + msgid);
70      return (null);
71
72
73   }
74
75
76
77 }
78
79
Popular Tags