// Composite pattern -- Structural example | |
using System; using System.Text; using System.Collections;
{ // Fields protected string name;
// Constructors public Component( string name ) { this.name = name; }
// Methods abstract public void Add(Component c); abstract public void Remove( Component c ); abstract public void Display( int depth ); }
{ // Fields private ArrayList children = new ArrayList();
// Constructors public Composite( string name ) : base( name ) {}
// Methods public override void Add( Component component ) { children.Add( component ); } public override void Remove( Component component ) { children.Remove( component ); } public override void Display( int depth ) { Console.WriteLine( new String( '-', depth ) + name );
// Display each of the node's children foreach( Component component in children ) component.Display( depth + 2 ); } }
{ // Constructors public Leaf( string name ) : base( name ) {}
// Methods public override void Add( Component c ) { Console.WriteLine("Cannot add to a leaf"); }
{ Console.WriteLine("Cannot remove from a leaf"); }
{ Console.WriteLine( new String( '-', depth ) + name ); } }
/// Client test /// </summary> public class Client { public static void Main( string[] args ) { // Create a tree structure Composite root = new Composite( "root" ); root.Add( new Leaf( "Leaf A" )); root.Add( new Leaf( "Leaf B" )); Composite comp = new Composite( "Composite X" );
comp.Add( new Leaf( "Leaf XB" ) ); root.Add( comp );
// Add and remove a leaf Leaf l = new Leaf( "Leaf D" ); root.Add( l ); root.Remove( l );
// Recursively display nodes root.Display( 1 ); } } | |
Output
|