001 package Jt;
002
003
004
005 /**
006 * Jt Implementation of the Facade design pattern.
007 */
008
009 public class JtFacade extends JtComposite {
010
011
012 public static final String JtCLASS_NAME = JtFacade.class.getName();
013 private static final long serialVersionUID = 1L;
014
015
016 public JtFacade () {
017 }
018
019
020 // Test this facade
021
022 private Object test () {
023 JtMessage msg, tmp;
024
025 // Broadcast the JtTEST message to all the subsystems
026 // of the facade
027
028 System.out.println
029 ("Sending a JtTEST message to all the subsystems of the facade ...");
030
031 msg = new JtMessage (JtObject.JtBROADCAST);
032
033 tmp = new JtMessage (JtObject.JtTEST);
034
035 msg.setMsgContent (tmp);
036
037
038 return (sendMessage (this, msg));
039
040 }
041
042 /**
043 * Process object messages.
044 * <ul>
045 * <li>JtREMOVES - Performs any housekeeping that may be needed before the object is removed.
046 * </ul>
047 */
048
049 public Object processMessage (Object event) {
050
051 String msgid = null;
052 JtMessage e = (JtMessage) event;
053 //Object content;
054 //Object data;
055
056
057 if (e == null)
058 return null;
059
060 msgid = (String) e.getMsgId ();
061
062 if (msgid == null)
063 return null;
064
065 //content = e.getMsgContent();
066 //data = e.getMsgData ();
067
068
069 if (msgid.equals (JtObject.JtTEST)) {
070 return (test ());
071 }
072
073 return (super.processMessage (event));
074
075
076 }
077
078
079 /**
080 * Demonstrates the messages processed by JtFacade.
081 */
082
083
084 public static void main(String[] args) {
085
086 JtFactory factory = new JtFactory ();
087 JtFacade facade;
088 JtEcho echo1;
089 JtEcho echo2;
090 JtMessage msg;
091
092 // Create an instance of JtFacade
093
094 facade = (JtFacade) factory.createObject (JtFacade.JtCLASS_NAME, "facade");
095
096
097 echo1 = (JtEcho) factory.createObject (JtEcho.JtCLASS_NAME, "echo1");
098 echo2 = (JtEcho) factory.createObject (JtEcho.JtCLASS_NAME, "echo2");
099
100
101 System.out.println ("JtFacade(JtADD_CHILD): adding a subsystem ...");
102
103 msg = new JtMessage (JtComposite.JtADD_CHILD);
104
105 factory.setValue (msg, "msgContent", echo1);
106 //factory.setValue (msg, "msgData", "echo1");
107
108 factory.sendMessage (facade, msg);
109
110
111 System.out.println ("JtFacade(JtADD_CHILD): adding a subsystem ...");
112
113 msg = new JtMessage (JtComposite.JtADD_CHILD);
114
115 factory.setValue (msg, "msgContent", echo2);
116 //factory.setValue (msg, "msgData", "echo2");
117
118 factory.sendMessage (facade, msg);
119
120
121 factory.sendMessage (facade, new JtMessage (JtObject.JtTEST));
122
123 factory.removeObject ("facade");
124
125
126 }
127
128
129 }
130
131