It has to deal with two types: Components and Composites of those components. Composite objects consist of Components.
Components:
- IComponent - default behavior for all object used in composition
- Component
- Composite - Implements the operations as applicable to composite objects. The Composite keeps as a list a local structure that consists of Components and Composites
Sample:
public interface IComponent
void Add(IComponent
IComponent
IComponent
string Display(int depth);
T Item {get; set;} }
// The Component
public class Component
public T Name {get; set;}
public Component (T name) { Name = name; }
public void Add(IComponent
public IComponent
public string Display(int depth) { return new String('-', depth) + Name+"\n"; }
public IComponent
// The Composite
public class Composite
List
public T Name {get; set;}
public Composite (T name) { Name = name;
list = new List
public void Add(IComponent
Component
// Finds the item from a particular point in the structure
// and returns the composite from which it was removed
// If not found, return the point as given
public IComponent
IComponent
if (holder!=null) { (holder as Composite
else return this; }
// Recursively looks for an item
// Returns its reference or else null
public IComponent
if (Name.Equals(s)) return this;
IComponent
foreach (IComponent
found = c.Find(s);
if (found!=null) break; }
return found; }
// Displays items in a format indicating their level in the composite structure
public string Display(int depth) { StringBuilder s = new StringBuilder(new String('-', depth));
s.Append("Set "+ Name + " length :" + list.Count + "\n");
foreach (IComponent
return s.ToString( ); } }
}
Niciun comentariu:
Trimiteți un comentariu