Skip to content

Commit

Permalink
Strategy Design Pattern
Browse files Browse the repository at this point in the history
A demo to illustrate the strategy design pattern
  • Loading branch information
hustwenchao committed Mar 27, 2016
1 parent c0dfd3b commit 2676078
Show file tree
Hide file tree
Showing 49 changed files with 871 additions and 0 deletions.
Binary file added Strategy/SimDuck/.vs/SimDuck/v14/.suo
Binary file not shown.
6 changes: 6 additions & 0 deletions Strategy/SimDuck/SimDuck/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
18 changes: 18 additions & 0 deletions Strategy/SimDuck/SimDuck/Duck/AfricanBlackDuck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using SimDuck.QuackBehavior;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck
{
public class AfricanBlackDuck : Duck
{
public AfricanBlackDuck()
{
quackBehavior = new Quack();
flyBehavior = new FlyWithWing();
}
}
}
28 changes: 28 additions & 0 deletions Strategy/SimDuck/SimDuck/Duck/Duck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace SimDuck
{
public class Duck
{
protected QuackBehavior.QuackBehavior quackBehavior;
protected FlyBehavior flyBehavior;

public virtual void PerformFly()
{
flyBehavior.DoFly();
}

public virtual void PeformQuack()
{
quackBehavior.DoQuack();
}

public void ShowDisplay()
{
Console.WriteLine(this.GetType());
PeformQuack();
PerformFly();
Console.WriteLine();
}
}
}
18 changes: 18 additions & 0 deletions Strategy/SimDuck/SimDuck/Duck/LittleYellowDuck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using SimDuck.QuackBehavior;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck
{
public class LittleYellowDuck : Duck
{
public LittleYellowDuck()
{
quackBehavior = new MuteQuack();
flyBehavior = new FlyNoWay();
}
}
}
18 changes: 18 additions & 0 deletions Strategy/SimDuck/SimDuck/Duck/RubberDuck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using SimDuck.QuackBehavior;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck
{
public class RubberDuck : Duck
{
public RubberDuck()
{
quackBehavior = new Squeak();
flyBehavior = new FlyNoWay();
}
}
}
17 changes: 17 additions & 0 deletions Strategy/SimDuck/SimDuck/FlyBehavior/FlyBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck
{
public class FlyBehavior
{
public virtual void DoFly()
{

}

}
}
16 changes: 16 additions & 0 deletions Strategy/SimDuck/SimDuck/FlyBehavior/FlyNoWay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck
{
public class FlyNoWay : FlyBehavior
{
public override void DoFly()
{
Console.WriteLine("I can not fly");
}
}
}
16 changes: 16 additions & 0 deletions Strategy/SimDuck/SimDuck/FlyBehavior/FlyWithWing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck
{
public class FlyWithWing : FlyBehavior
{
public override void DoFly()
{
Console.WriteLine("Fly with Wing");
}
}
}
24 changes: 24 additions & 0 deletions Strategy/SimDuck/SimDuck/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck
{
class Program
{
static void Main(string[] args)
{
Duck[] duckArray = new Duck[3];
duckArray[0] = new AfricanBlackDuck();
duckArray[1] = new LittleYellowDuck();
duckArray[2] = new RubberDuck();
for(int i = 0; i < duckArray.Length; i++)
{
duckArray[i].ShowDisplay();
}
Console.Read();
}
}
}
36 changes: 36 additions & 0 deletions Strategy/SimDuck/SimDuck/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("SimDuck")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SimDuck")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

//将 ComVisible 设置为 false 将使此程序集中的类型
//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("72763a8b-5264-4e3b-931c-7528e2457ccd")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
16 changes: 16 additions & 0 deletions Strategy/SimDuck/SimDuck/QuackBehavior/MuteQuack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck.QuackBehavior
{
public class MuteQuack : QuackBehavior
{
public override void DoQuack()
{
Console.WriteLine("Can't Quack");
}
}
}
16 changes: 16 additions & 0 deletions Strategy/SimDuck/SimDuck/QuackBehavior/Quack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck.QuackBehavior
{
public class Quack : QuackBehavior
{
public override void DoQuack()
{
Console.WriteLine("Quack With Mouth");
}
}
}
16 changes: 16 additions & 0 deletions Strategy/SimDuck/SimDuck/QuackBehavior/QuackBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck.QuackBehavior
{
public class QuackBehavior
{
public virtual void DoQuack()
{

}
}
}
16 changes: 16 additions & 0 deletions Strategy/SimDuck/SimDuck/QuackBehavior/Squeak.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimDuck.QuackBehavior
{
public class Squeak : QuackBehavior
{
public override void DoQuack()
{
Console.WriteLine("Squeak Squeak Squeak");
}
}
}
112 changes: 112 additions & 0 deletions Strategy/SimDuck/SimDuck/SimDuck.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{72763A8B-5264-4E3B-931C-7528E2457CCD}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SimDuck</RootNamespace>
<AssemblyName>SimDuck</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>1</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>4CC3A0DF6134FAA6E6B1D13261B7E142B957DA51</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>SimDuck_TemporaryKey.pfx</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>true</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<SignManifests>true</SignManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Duck\AfricanBlackDuck.cs" />
<Compile Include="Duck\Duck.cs" />
<Compile Include="FlyBehavior\FlyBehavior.cs" />
<Compile Include="FlyBehavior\FlyNoWay.cs" />
<Compile Include="FlyBehavior\FlyWithWing.cs" />
<Compile Include="Duck\LittleYellowDuck.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QuackBehavior\MuteQuack.cs" />
<Compile Include="QuackBehavior\Quack.cs" />
<Compile Include="QuackBehavior\QuackBehavior.cs" />
<Compile Include="QuackBehavior\Squeak.cs" />
<Compile Include="Duck\RubberDuck.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="SimDuck_TemporaryKey.pfx" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.5.2">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.5.2 %28x86 和 x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit 2676078

Please sign in to comment.