class Client public void Main Component root new Composite Root Compon

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Client
{
public void Main()
{
Component root = new Composite("Root");
Component leaf = new Leaf("Leaf");
Composite subtree = new Composite("Subtree");
root.Add(leaf);
root.Add(subtree);
root.Display();
}
}
abstract class Component
{
protected string name;
public Component(string name)
{
this.name = name;
}
public abstract void Display();
public abstract void Add(Component c);
public abstract void Remove(Component c);
}
class Composite : Component
{
List<Component> children = new List<Component>();
public Composite(string name)
: base(name)
{}
public override void Add(Component component)
{
children.Add(component);
}
public override void Remove(Component component)
{
children.Remove(component);
}
public override void Display()
{
Console.WriteLine(name);
foreach (Component component in children)
{
component.Display();
}
}
}
class Leaf : Component
{
public Leaf(string name)
: base(name)
{}
public override void Display()
{
Console.WriteLine(name);
}
public override void Add(Component component)
{
throw new NotImplementedException();
}
public override void Remove(Component component)
{
throw new NotImplementedException();
}
}