Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 200491.3913 Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.

Similar presentations


Presentation on theme: "March 200491.3913 Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat."— Presentation transcript:

1 March 200491.3913 Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat individual objects and compositions of objects uniformly

2 March 200491.3913 Ron McFadyen2 Composite Component declares the interface for objects implements default behavior Leaf represents leaf objects in the composition. has no children. defines behavior for primitive objects in the composition. Composite defines behavior for components with children stores child components implements child-related operations Client manipulates objects using the interface

3 March 200491.3913 Ron McFadyen3 Composite – general code for a Component abstract class Component { protected string name; public Component( string name ) { this.name = name; } abstract public void Add(Component c); abstract public void Remove( Component c ); abstract public void Display( int depth ); }

4 March 200491.3913 Ron McFadyen4 Composite – general code for a composite class Composite : Component { private ArrayList children = new ArrayList(); public Composite( string name ) : base( name ) {} public override void Add( Component component ) { children.Add( component ); } public override void Remove( Component component ) { children.Remove( component ); }

5 March 200491.3913 Ron McFadyen5 Composite – general code for a composite 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 ); } }

6 March 200491.3913 Ron McFadyen6 Composite – general code for a leaf class Leaf : Component { public Leaf( string name ) : base( name ) {} public override void Add( Component c ) { Console.WriteLine("Cannot add to a leaf"); } public override void Remove( Component c ) { Console.WriteLine("Cannot remove from a leaf"); } public override void Display( int depth ) { Console.WriteLine( new String( '-', depth ) + name ); } }

7 March 200491.3913 Ron McFadyen7 Composite – general code for a client public class Client { public static void Main( string[] args ) { // Create a tree 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 XA" ) ); comp.Add( new Leaf( "Leaf XB" ) ); root.Add( comp );

8 March 200491.3913 Ron McFadyen8 Composite – general code for a client root.Add( new Leaf( "Leaf C" )); // Add and remove a leaf Leaf l = new Leaf( "Leaf D" ); root.Add( l ); root.Remove( l ); // Recursively display nodes root.Display( 1 ); } } What hierarchy has been created?


Download ppt "March 200491.3913 Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat."

Similar presentations


Ads by Google