jtcomposite.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 357 行
JAVA
357 行
package Jt;
import java.util.*;
/**
* Jt Implementation of the Composite pattern.
*/
public class JtComposite extends JtPrototype {
public static final String JtCLASS_NAME = JtComposite.class.getName();
private static final long serialVersionUID = 1L;
private Hashtable hashtable = null;
public static final String JtREMOVE_CHILD = "JtREMOVE_CHILD";
public static final String JtADD_CHILD = "JtADD_CHILD";
public static final String JtGET_CHILD = "JtGET_CHILD";
public Iterator iterator;
/**
* Changes the hastable used to represent this class and store component objects.
*/
public void setHashtable (Hashtable hashtable) {
this.hashtable = hashtable;
}
/**
* Returns the hastable used to represent this class and store component objects.
*/
public Hashtable getHashtable () {
return (hashtable);
}
public Iterator getIterator () {
Collection col;
if (hashtable == null)
return null;
col = hashtable.values();
if (col == null)
return null;
return (col.iterator());
}
public void setIterator () {
}
public JtComposite () {
}
// Broadcast a message
private void broadcastMessage (JtMessage msg)
{
Collection values;
Iterator it;
Object obj;
JtMessage msg1;
if (msg == null || hashtable == null)
return;
values = hashtable.values ();
if (values == null)
return;
synchronized (hashtable) {
it = values.iterator ();
while (it.hasNext()) {
obj = it.next ();
if (!(obj instanceof JtInterface))
continue;
if (!(obj instanceof JtComposite)) {
sendMessage (obj, msg);
continue;
}
msg1 = new JtMessage (JtObject.JtREMOVE);
msg1.setMsgContent(msg);
sendMessage (obj, msg1);
}
}
}
/**
* Process object messages.
* <ul>
* <li> JtADD_CHILD - Adds the object specified by msgContent to this composition.
* If the object is an instance JtObject, it is stored using the object name (objName) as the key.
* Otherwise obj.toString () is used as the key.
* <li> JtREMOVE_CHILD - Removes an object from this composition. msgContent specifies
* the key needed to remove the object.
* <li> JtGET_CHILD - Returns the object specified by msgContent (key) or null if it
* doesn't exist.
* <li> JtBROADCAST - Broadcast the message specified by msgContent to all the objects
* in this composition.
* <li>JtREMOVE - Broadcast JtREMOVE to all the objects in this composition.
* </ul>
* @param message Jt Message
*/
public Object processMessage (Object message) {
String msgid = null;
JtMessage e = (JtMessage) message;
Object content;
//Object data;
if (e == null)
return null;
msgid = (String) e.getMsgId ();
if (msgid == null)
return null;
content = e.getMsgContent();
//data = e.getMsgData ();
// Remove the composite
if (msgid.equals (JtObject.JtREMOVE)) {
// send JtREMOVE to all the members of the composite
broadcastMessage (new JtMessage (JtObject.JtREMOVE));
return (null);
}
if (msgid.equals (JtComposite.JtADD_CHILD)) {
// Add an object to the hash table
if (content == null) {
handleWarning
("JtComposite.processMessage(JtADD_CHILD):invalid value (null)");
return (null);
}
if (hashtable == null)
hashtable = new Hashtable ();
hashtable.put (content.toString(), content);
return (null);
}
if (msgid.equals (JtComposite.JtREMOVE_CHILD)) {
// Add an object to the hash table
if (content == null) {
handleWarning
("JtComposite.processMessage(JtREMOVE_CHILD):invalid value (null)");
return (null);
}
if (hashtable == null)
hashtable = new Hashtable ();
if (content instanceof String)
return (hashtable.remove (content));
else
return (hashtable.remove (content.toString()));
}
if (msgid.equals (JtComposite.JtGET_CHILD)) {
if (content == null) {
handleWarning
("JtComposite.processMessage(JtGET):invalid value (null)");
return (null);
}
if (hashtable == null)
return (null);
if (content instanceof String)
return (hashtable.get (content));
else
return (hashtable.get (content.toString()));
}
if (msgid.equals (JtObject.JtTEST)) {
return (test ());
}
// Broadcast a message to all the members
// of the hash table
if (msgid.equals (JtObject.JtBROADCAST)) {
if (hashtable == null) {
return (null);
}
broadcastMessage ((JtMessage) content);
return (null);
}
return (super.processMessage (message));
}
/**
* Unit tests the messages processed by JtComposite and illustrates the use of this class.
*/
private Object test() {
JtObject main = new JtFactory ();
JtMessage msg;
JtEcho echo1;
JtEcho echo2;
echo1 = (JtEcho) createObject (JtEcho.JtCLASS_NAME, "echo1");
echo2 = (JtEcho) createObject (JtEcho.JtCLASS_NAME, "echo2");
System.out.println ("JtComposite(JtADD_CHILD): adding a child ...");
msg = new JtMessage (JtComposite.JtADD_CHILD);
setValue (msg, "msgContent", echo1);
sendMessage (this, msg);
System.out.println ("JtComposite(JtADD_CHILD): adding a child ...");
msg = new JtMessage (JtComposite.JtADD_CHILD);
setValue (msg, "msgContent", echo2);
sendMessage (this, msg);
msg = new JtMessage (JtComposite.JtGET_CHILD);
main.setValue (msg, "msgContent", "echo1");
System.err.println ("JtComposite(JtGET_CHILD):" +
sendMessage (this, msg));
System.out.println ("Printing the composition (XML format) ...");
sendMessage (this, new JtMessage (JtComposite.JtPRINT));
msg = new JtMessage (JtComposite.JtREMOVE_CHILD);
main.setValue (msg, "msgContent", "echo1");
System.err.println ("JtComposite(JtREMOVE_CHILD):" +
sendMessage (this, msg));
msg = new JtMessage (JtObject.JtBROADCAST);
msg.setMsgContent (new JtMessage (JtObject.JtPRINT));
System.out.println
("JtComposite(JtBROADCAST): broadcasting a message to the composition ...");
sendMessage (this, msg);
return (this);
}
/**
* Demonstrates the messages processed by JtComposite
*/
public static void main(String[] args) {
JtObject factory = new JtFactory ();
JtComposite composite, composite1;
Double leaf1 = new Double (1.0);
Double leaf2 = new Double (2.0);
Double leaf3 = new Double (3.0);
JtMessage msg;
// Create an instance of JtComposite
composite = (JtComposite) factory.createObject (JtComposite.JtCLASS_NAME, "composite");
// Add objects to the composite
msg = new JtMessage (JtComposite.JtADD_CHILD);
msg.setMsgContent(leaf1);
factory.sendMessage(composite, msg);
msg.setMsgContent(leaf2);
factory.sendMessage(composite, msg);
composite1 = (JtComposite) factory.createObject (JtComposite.JtCLASS_NAME, "composite1");
msg.setMsgContent(leaf3);
factory.sendMessage(composite1, msg);
msg = new JtMessage (JtComposite.JtADD_CHILD);
msg.setMsgContent(composite1);
factory.sendMessage(composite, msg);
// Print the composite (XML format)
factory.sendMessage(composite, new JtMessage (JtObject.JtPRINT));
// Manipulate the composite
msg = new JtMessage (JtComposite.JtGET_CHILD);
msg.setMsgContent(leaf1.toString());
System.out.println ("leaf1 = " + factory.sendMessage(composite, msg));
msg = new JtMessage (JtComposite.JtREMOVE_CHILD);
msg.setMsgContent(leaf2.toString());
factory.sendMessage(composite, msg);
// Print the composite (XML format)
factory.sendMessage(composite, new JtMessage (JtObject.JtPRINT));
factory.sendMessage (composite, new JtMessage (JtObject.JtTEST));
factory.removeObject ("composite");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?