1 2 3 package Jt; 4 import java.util.*; 5 import java.lang.reflect.*; 6 import java.beans.*; 7 import java.io.*; 8 9 12 13 public class JtSingleton extends JtObject { 14 private static Object instance; 15 16 17 18 public JtSingleton () { 19 } 20 21 24 25 public static synchronized Object getInstance() 26 { 27 28 return instance; 29 } 30 31 32 33 36 37 public static synchronized void setInstance(Object newInstance) 38 { 39 if (instance == null) 40 instance = newInstance; 41 42 } 43 44 45 52 53 public Object processMessage (Object event) { 54 55 String msgid = null; 56 JtMessage e = (JtMessage) event; 57 Object content; 58 Object data; 59 60 61 if (e == null) 62 return null; 63 64 msgid = (String ) e.getMsgId (); 65 66 if (msgid == null) 67 return null; 68 69 content = e.getMsgContent(); 70 72 73 if (msgid.equals ("JtREMOVE")) { 74 return (super.processMessage (null)); 75 } 76 77 78 handleError ("JtSingleton.processMessage: invalid message id:" + msgid); 79 return (null); 80 81 } 82 83 84 87 88 public static void main(String [] args) { 89 90 JtFactory main = new JtFactory (); 91 JtMessage msg, msg1; 92 Integer count; 93 JtSingleton singleton, singleton1; 94 95 System.out.println ("Creating an instance of a singleton ..."); 96 97 99 singleton = (JtSingleton) main.createObject ("Jt.JtSingleton", "singleton"); 100 101 102 System.out.println ("Attempting to create a second instance of a singleton ..."); 103 104 singleton1 = (JtSingleton) main.createObject ("Jt.JtSingleton", "singleton1"); 105 106 main.removeObject ("singleton"); 107 108 109 } 110 111 } 112 113 114 | Popular Tags |