-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (46 loc) · 1.11 KB
/
Program.cs
File metadata and controls
57 lines (46 loc) · 1.11 KB
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
using System;
using System.Collections.Generic;
namespace Composite;
public interface IGraphic
{
void Draw();
}
public class Dot(int x, int y) : IGraphic
{
public void Draw()
{
Console.WriteLine($"Drawing dot at ({x},{y})");
}
}
public class CompositeGraphic(string name) : IGraphic
{
private readonly List<IGraphic> children = [];
public string Name { get; } = name;
public void Add(IGraphic graphic) => children.Add(graphic);
public void Draw()
{
Console.WriteLine($"Group {Name} contains {children.Count} items");
foreach (var child in children)
{
child.Draw();
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Composite Pattern Demo ===");
Console.WriteLine();
var circleGroup = new CompositeGraphic("CircleGroup");
circleGroup.Add(new Dot(1, 1));
circleGroup.Add(new Dot(2, 2));
var drawing = new CompositeGraphic("Drawing");
drawing.Add(circleGroup);
drawing.Add(new Dot(5, 5));
drawing.Draw();
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}