-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrategyPattern.cs
67 lines (59 loc) · 1.67 KB
/
StrategyPattern.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
using System;
using System.Collections.Generic;
namespace CodeSamples.Patterns.Behavioral
{
#region Strategy classes and interfaces
interface ISortStrategy
{
void Sort(List<int> sortableList);
}
class QuickSortStrategy : ISortStrategy
{
public void Sort(List<int> sortableList)
{
Console.WriteLine($"Quick Sort Called...");
}
}
class ShellSortStrategy : ISortStrategy
{
public void Sort(List<int> sortableList)
{
Console.WriteLine($"Shell Sort Called...");
}
}
class InsertionSortStrategy : ISortStrategy
{
public void Sort(List<int> sortableList)
{
Console.WriteLine($"Insertion Sort Called...");
}
}
#endregion
class StrategyTestClass
{
private ISortStrategy _sortStrategy = null;
private readonly List<int> _sortableList = new List<int>() { 5, 3, 7, 12, 100, 1 };
public void SetSortStrategy(ISortStrategy sortStrategy)
{
_sortStrategy = sortStrategy;
}
public void Sort()
{
_sortStrategy.Sort(_sortableList);
}
}
public class StrategyPatternSample : SampleExecute
{
public override void Execute()
{
Section("Strategy Pattern");
var testClass = new StrategyTestClass();
testClass.SetSortStrategy(new QuickSortStrategy());
testClass.Sort();
testClass.SetSortStrategy(new ShellSortStrategy());
testClass.Sort();
testClass.SetSortStrategy(new InsertionSortStrategy());
testClass.Sort();
}
}
}