-
Notifications
You must be signed in to change notification settings - Fork 31
/
TemplateMethod.cs
118 lines (109 loc) · 3.22 KB
/
TemplateMethod.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace DesignPatterns.Behavioral;
public class TemplateMethod
{
[Fact]
public void Execute()
{
OrderedCollection ascendingCollection = new AscendingCollection
{
3,
4,
2,
8,
1,
5,
3,
6,
9,
7
};
Assert.Equal(new[] { 1, 2, 3, 3, 4, 5, 6, 7, 8, 9 }, ascendingCollection);
OrderedCollection descendingCollection = new DescendingCollection
{
3,
4,
2,
8,
1,
5,
3,
6,
9,
7
};
Assert.Equal(new[] { 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 }, descendingCollection);
}
/// <summary>
/// Abstract class
/// </summary>
/// <remarks>
/// - Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
/// - Implements a template method defining the skeleton of an algorithm.
/// - The template method calls primitive operations as well as operations defined in AbstractClass or those of other
/// objects.
/// </remarks>
public abstract class OrderedCollection : IEnumerable<int>
{
private readonly List<int> _collection = [];
/// <summary>
/// Template Method
/// </summary>
/// <param name="value"></param>
public void Add(int value)
{
int index = FindIndex(_collection, value);
_collection.Insert(index, value);
}
/// <summary>
/// Primitive Operation
/// </summary>
/// <param name="source" />
/// <param name="value" />
/// <returns></returns>
protected abstract int FindIndex(IReadOnlyList<int> source, int value);
public IEnumerator<int> GetEnumerator() => _collection.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_collection).GetEnumerator();
}
/// <summary>
/// Concrete Class
/// </summary>
/// <remarks>
/// Implements the primitive operations to carry out subclass-specific steps of the algorithm.
/// </remarks>
public class AscendingCollection : OrderedCollection
{
/// <inheritdoc />
protected override int FindIndex(IReadOnlyList<int> source, int value)
{
for (int index = source.Count; index > 0; index--)
{
if (value >= source[index - 1])
{
return index;
}
}
return 0;
}
}
/// <summary>
/// Concrete Class
/// </summary>
/// <remarks>
/// Implements the primitive operations to carry out subclass-specific steps of the algorithm.
/// </remarks>
public class DescendingCollection : OrderedCollection
{
/// <inheritdoc />
protected override int FindIndex(IReadOnlyList<int> source, int value)
{
for (var index = 0; index < source.Count; index++)
{
if (value >= source[index])
{
return index;
}
}
return source.Count;
}
}
}