Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65d64aa

Browse files
Strategy pattern example
1 parent dc36677 commit 65d64aa

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

‎DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObserverPattern", "Observer
4141
EndProject
4242
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisitorPattern", "VisitorPattern\VisitorPattern.csproj", "{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}"
4343
EndProject
44+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyPattern", "StrategyPattern\StrategyPattern.csproj", "{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}"
45+
EndProject
4446
Global
4547
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4648
Debug|Any CPU = Debug|Any CPU
@@ -123,6 +125,10 @@ Global
123125
{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
124126
{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
125127
{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Release|Any CPU.Build.0 = Release|Any CPU
128+
{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
129+
{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Debug|Any CPU.Build.0 = Debug|Any CPU
130+
{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Release|Any CPU.ActiveCfg = Release|Any CPU
131+
{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Release|Any CPU.Build.0 = Release|Any CPU
126132
EndGlobalSection
127133
GlobalSection(SolutionProperties) = preSolution
128134
HideSolutionNode = FALSE

‎StrategyPattern/Program.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace StrategyPattern
5+
{
6+
interface ISortStrategy
7+
{
8+
List<int> Sort(List<int> dataset);
9+
}
10+
11+
class BubbleSortStrategy : ISortStrategy
12+
{
13+
public List<int> Sort(List<int> dataset)
14+
{
15+
Console.WriteLine("Sorting using Bubble Sort !");
16+
return dataset;
17+
}
18+
}
19+
20+
class QuickSortStrategy : ISortStrategy
21+
{
22+
public List<int> Sort(List<int> dataset)
23+
{
24+
Console.WriteLine("Sorting using Quick Sort !");
25+
return dataset;
26+
}
27+
}
28+
29+
class Sorter
30+
{
31+
private readonly ISortStrategy mSorter;
32+
33+
public Sorter(ISortStrategy sorter)
34+
{
35+
mSorter = sorter;
36+
}
37+
38+
public List<int> Sort(List<int> unSortedList)
39+
{
40+
return mSorter.Sort(unSortedList);
41+
}
42+
}
43+
44+
class Program
45+
{
46+
static void Main(string[] args)
47+
{
48+
var unSortedList = new List<int> { 1, 10, 2, 16, 19 };
49+
50+
var sorter = new Sorter(new QuickSortStrategy());
51+
sorter.Sort(unSortedList); // // Output : Sorting using Bubble Sort !
52+
53+
sorter = new Sorter(new QuickSortStrategy());
54+
sorter.Sort(unSortedList); // // Output : Sorting using Quick Sort !
55+
56+
Console.ReadLine();
57+
}
58+
}
59+
}

‎StrategyPattern/StrategyPattern.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /