Skip to content

Commit 7c5e014

Browse files
committed
Add Composite Design Pattern implementation
Implemented the Composite Design Pattern, including the `IGraphic` interface, `Dot`, `CircleGraphic`, and `CompositeGraphic` classes. Updated `Program.cs` to demonstrate the pattern with examples of flat and nested composite structures. Updated `README.md` to document the Composite pattern, its purpose, usage, and related files. Added unit tests in `CompositeTests.cs` to verify functionality, including nested composites. Also included a demonstration of the Singleton pattern in `Program.cs` using the `Logger` class.
1 parent e5d68b8 commit 7c5e014

File tree

7 files changed

+91
-1
lines changed

7 files changed

+91
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace DesignPatterns.Composite;
2+
3+
public sealed class CircleGraphic : IGraphic
4+
{
5+
public string Draw() => "Circle";
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace DesignPatterns.Composite;
2+
3+
public sealed class CompositeGraphic : IGraphic
4+
{
5+
private readonly List<IGraphic> _children = new();
6+
7+
public void Add(IGraphic g) => _children.Add(g);
8+
public void Remove(IGraphic g) => _children.Remove(g);
9+
10+
public string Draw()
11+
{
12+
return string.Join(", ", _children.Select(c => c.Draw()));
13+
}
14+
}

DesignPatterns/Composite/Dot.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace DesignPatterns.Composite;
2+
3+
public sealed class Dot : IGraphic
4+
{
5+
public string Draw() => "Dot";
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace DesignPatterns.Composite;
2+
3+
public interface IGraphic
4+
{
5+
string Draw();
6+
}

DesignPatterns/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using DesignPatterns.AbstractFactory;
66
using DesignPatterns.Adapter;
77
using DesignPatterns.Bridge;
8+
using DesignPatterns.Composite;
89

910
Console.WriteLine("Factory pattern demo:");
1011

@@ -76,6 +77,21 @@
7677
adv.Mute();
7778
Console.WriteLine($"Radio - Enabled: {radio.IsEnabled}, Volume: {radio.Volume}, Channel: {radio.Channel}");
7879

80+
// Composite demo
81+
Console.WriteLine();
82+
Console.WriteLine("Composite pattern demo:");
83+
var root = new CompositeGraphic();
84+
root.Add(new Dot());
85+
root.Add(new CircleGraphic());
86+
Console.WriteLine(root.Draw());
87+
88+
var nested = new CompositeGraphic();
89+
var child = new CompositeGraphic();
90+
child.Add(new Dot());
91+
nested.Add(child);
92+
nested.Add(new CircleGraphic());
93+
Console.WriteLine(nested.Draw());
94+
7995
// Singleton demo
8096
Logger.Instance.Clear();
8197
Logger.Instance.Log("Singleton started");

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ Design Patterns Examples
33
This solution demonstrates simple design pattern examples in C# targeting .NET 10 (C# 14).
44

55
Projects
6-
- `DesignPatterns` - Console demo showing Factory, Singleton, Builder, Prototype, Abstract Factory, Adapter, and Bridge patterns.
6+
- `DesignPatterns` - Console demo showing Factory, Singleton, Builder, Prototype, Abstract Factory, Adapter, Bridge, and Composite patterns.
77
- Factory implementation: `DesignPatterns/Factories`
88
- Singleton implementation: `DesignPatterns/Singleton/Logger.cs`
99
- Builder implementation: `DesignPatterns/Builder/HouseBuilder.cs` and `DesignPatterns/Builder/House.cs`
1010
- Prototype implementation: `DesignPatterns/Prototype/Person.cs` and `DesignPatterns/Prototype/Address.cs`
1111
- Abstract Factory implementation: `DesignPatterns/AbstractFactory/*`
1212
- Adapter implementation: `DesignPatterns/Adapter/*`
1313
- Bridge implementation: `DesignPatterns/Bridge/*`
14+
- Composite implementation: `DesignPatterns/Composite/*`
1415
- `Tests` - xUnit tests covering the examples.
1516

1617
Included patterns and brief docs
@@ -93,6 +94,18 @@ Included patterns and brief docs
9394
remote.SetChannel(10);
9495
```
9596
97+
- Composite Pattern
98+
- Purpose: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions uniformly.
99+
- Example: `DesignPatterns/Composite/*` contains `IGraphic` (leaf `Dot`, `CircleGraphic`) and `CompositeGraphic` which aggregates children.
100+
- Usage snippet:
101+
102+
```csharp
103+
var root = new CompositeGraphic();
104+
root.Add(new Dot());
105+
root.Add(new CircleGraphic());
106+
Console.WriteLine(root.Draw()); // "Dot, Circle"
107+
```
108+
96109
Tests
97110
- Tests are written with xUnit in the `Tests` project.
98111
- Factory tests: `Tests/ShapeFactoryTests.cs`
@@ -102,6 +115,7 @@ Tests
102115
- Abstract Factory tests: `Tests/AbstractFactoryTests.cs`
103116
- Adapter tests: `Tests/AdapterTests.cs`
104117
- Bridge tests: `Tests/BridgeTests.cs`
118+
- Composite tests: `Tests/CompositeTests.cs`
105119
106120
Requirements
107121
- .NET 10 SDK

Tests/CompositeTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using DesignPatterns.Composite;
2+
3+
namespace Tests;
4+
5+
public class CompositeTests
6+
{
7+
[Fact]
8+
public void Composite_DrawsAllChildren()
9+
{
10+
var composite = new CompositeGraphic();
11+
composite.Add(new Dot());
12+
composite.Add(new CircleGraphic());
13+
14+
Assert.Equal("Dot, Circle", composite.Draw());
15+
}
16+
17+
[Fact]
18+
public void Composite_CanNestComposites()
19+
{
20+
var root = new CompositeGraphic();
21+
var child = new CompositeGraphic();
22+
child.Add(new Dot());
23+
root.Add(child);
24+
root.Add(new CircleGraphic());
25+
26+
Assert.Equal("Dot, Circle", root.Draw());
27+
}
28+
}

0 commit comments

Comments
 (0)