216 | Chapter 6: Composite Pattern
Executing the statements in Example 6-7 will generate the following output. The
operation( ) method traverses the tree structure recursively using a depth-first
approach, and prints the component names accordingly.
display tree
============
root
composite 1
leaf 1
leaf 2
composite 2
leaf 3
leaf 4
leaf 5
leaf 6
remove first child of the second child of root
==============================================
root
composite 1
leaf 1
leaf 2
composite 2
leaf 4
leaf 5
leaf 6
remove the second child of root
===============================
root
composite 1
leaf 1
leaf 2
leaf 6
You can easily visualize what happens to the component tree by looking at what
happens graphically in Figure 6-5.
trace("display tree");
trace("============");
root.operation( );
trace("remove first child of the second child of root");
trace("==============================================");
root.getChild(2).remove(root.getChild(2).getChild(1));
root.operation( );
trace("remove the second child of root");
trace("===============================");
root.remove(root.getChild(2));
root.operation( ); }
}
}
Example 6-7. Main.as (client code to remove nodes) (continued)