-
Notifications
You must be signed in to change notification settings - Fork 31
/
Composite.cs
65 lines (55 loc) · 1.78 KB
/
Composite.cs
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
namespace DesignPatterns.Structural;
public class Composite
{
[Fact]
public void Execute()
{
IMovie firstMovie = new Movie("Slow and Safe");
Assert.Equal("Slow and Safe", firstMovie.Title);
IMovie secondMovie = new Movie("The Beginnator");
Assert.Equal("The Beginnator", secondMovie.Title);
var movieBundle = new MovieBundle(firstMovie);
Assert.Equal("Bundle of: Slow and Safe", movieBundle.Title);
movieBundle.AddMovie(secondMovie);
Assert.Equal("Bundle of: Slow and Safe, The Beginnator", movieBundle.Title);
}
/// <summary>
/// Component
/// </summary>
/// <remarks>
/// - Is the abstraction for all components, including composite ones.
/// - Declares the interface for objects in the composition.
/// </remarks>
public interface IMovie
{
string Title { get; }
}
/// <summary>
/// Leaf
/// </summary>
/// <remarks>
/// - Represents leaf objects in the composition.
/// - Implements all Component methods.
/// </remarks>
public class Movie(string title) : IMovie
{
public string Title => title;
}
/// <summary>
/// Composite
/// </summary>
/// <remarks>
/// - Represents a Component that has children.
/// - Implements methods to manipulate children.
/// - Implements all Component methods, generally by delegating them to its children.
/// </remarks>
public class MovieBundle(params IMovie[] movies) : IMovie
{
private readonly List<IMovie> _movies = [..movies];
public string Title => $"Bundle of: {string.Join(", ", _movies.Select(movie => movie.Title))}";
public void AddMovie(params IMovie[] movies)
{
_movies.AddRange(movies);
}
}
}