Skip to content

Commit 87ad823

Browse files
committed
add Strategy pattern
1 parent 9f43976 commit 87ad823

9 files changed

+187
-0
lines changed

Behavioral/Strategy/AddStrategy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Strategy {
2+
3+
public class AddStrategy : IStrategy {
4+
5+
public int Execute(int a, int b) {
6+
return a + b;
7+
}
8+
}
9+
}

Behavioral/Strategy/Client.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace Strategy {
4+
5+
class Client {
6+
7+
static void Main(string[] args) {
8+
9+
Context context;
10+
11+
// Three contexts following different strategies
12+
context = new Context(new AddStrategy());
13+
var resultA = context.ExecuteStrategy(3, 4);
14+
Console.WriteLine(resultA);
15+
16+
context = new Context(new SubstractStrategy());
17+
var resultB = context.ExecuteStrategy(3, 4);
18+
Console.WriteLine(resultB);
19+
20+
context = new Context(new MultiplyStrategy());
21+
var resultC = context.ExecuteStrategy(3, 4);
22+
Console.WriteLine(resultC);
23+
24+
Console.ReadKey();
25+
}
26+
}
27+
}

Behavioral/Strategy/Context.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Strategy {
2+
3+
public class Context {
4+
5+
private readonly IStrategy _strategy;
6+
7+
public Context(IStrategy strategy) {
8+
this._strategy = strategy;
9+
}
10+
11+
public int ExecuteStrategy(int a, int b) {
12+
return this._strategy.Execute(a, b);
13+
}
14+
}
15+
}

Behavioral/Strategy/IStrategy.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Strategy {
2+
3+
public interface IStrategy {
4+
5+
int Execute(int a, int b);
6+
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Strategy {
2+
3+
public class MultiplyStrategy : IStrategy {
4+
5+
public int Execute(int a, int b) {
6+
return a * b;
7+
}
8+
}
9+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Strategy")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Strategy")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("2b3f684e-ce46-4d48-822a-1ed0326f7aa0")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Behavioral/Strategy/Strategy.csproj

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Strategy</RootNamespace>
12+
<AssemblyName>Strategy</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<PropertyGroup>
37+
<StartupObject>Strategy.Client</StartupObject>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="AddStrategy.cs" />
50+
<Compile Include="Context.cs" />
51+
<Compile Include="IStrategy.cs" />
52+
<Compile Include="Client.cs" />
53+
<Compile Include="MultiplyStrategy.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
<Compile Include="SubstractStrategy.cs" />
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59+
Other similar extension points exist, see Microsoft.Common.targets.
60+
<Target Name="BeforeBuild">
61+
</Target>
62+
<Target Name="AfterBuild">
63+
</Target>
64+
-->
65+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Strategy {
2+
3+
public class SubstractStrategy : IStrategy {
4+
5+
public int Execute(int a, int b) {
6+
return a - b;
7+
}
8+
}
9+
}

DesignPatterns.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Observer", "Behavioral\Obse
1919
EndProject
2020
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "State", "Behavioral\State\State.csproj", "{F214B483-E8BB-477D-807F-24F176B588D5}"
2121
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategy", "Behavioral\Strategy\Strategy.csproj", "{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -77,6 +79,12 @@ Global
7779
{F214B483-E8BB-477D-807F-24F176B588D5}.Release|Any CPU.ActiveCfg = Release|x86
7880
{F214B483-E8BB-477D-807F-24F176B588D5}.Release|x86.ActiveCfg = Release|x86
7981
{F214B483-E8BB-477D-807F-24F176B588D5}.Release|x86.Build.0 = Release|x86
82+
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Debug|Any CPU.ActiveCfg = Debug|x86
83+
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Debug|x86.ActiveCfg = Debug|x86
84+
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Debug|x86.Build.0 = Debug|x86
85+
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|Any CPU.ActiveCfg = Release|x86
86+
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|x86.ActiveCfg = Release|x86
87+
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|x86.Build.0 = Release|x86
8088
EndGlobalSection
8189
GlobalSection(SolutionProperties) = preSolution
8290
HideSolutionNode = FALSE
@@ -90,5 +98,6 @@ Global
9098
{C404876C-B04B-451F-B159-2C0CF8CB861B} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
9199
{2823F21D-D474-447D-9C8B-74B29D0D9B35} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
92100
{F214B483-E8BB-477D-807F-24F176B588D5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
101+
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
93102
EndGlobalSection
94103
EndGlobal

0 commit comments

Comments
 (0)