-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentComposite.cs
More file actions
52 lines (42 loc) · 941 Bytes
/
Copy pathComponentComposite.cs
File metadata and controls
52 lines (42 loc) · 941 Bytes
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
public interface IComponent
{
void Display(int depth);
}
public class Leaf : IComponent
{
private string _name;
public Leaf(string name)
{
_name = name;
}
public void Display(int depth)
{
Console.WriteLine(new string('-', depth) + _name);
}
}
public class Composite : IComponent
{
private string _name;
private List<IComponent> _children = new List<IComponent>();
public Composite(string name)
{
_name = name;
}
public void Add(IComponent component)
{
_children.Add(component);
}
public void Remove(IComponent component)
{
_children.Remove(component);
}
public void Display(int depth)
{
Console.WriteLine(new string('-', depth) + _name);
// Recursively display child nodes
foreach (var component in _children)
{
component.Display(depth + 2);
}
}
}