Add strategy pattern basic example
This commit is contained in:
committed by
Petrutiu Mihai
parent
b5997ced99
commit
63c5ae3ab1
@@ -26,6 +26,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ObserverPattern", "src\Obse
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StatePattern", "src\StatePattern\StatePattern.xproj", "{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}"
|
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StatePattern", "src\StatePattern\StatePattern.xproj", "{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StrategyPattern", "src\StrategyPattern\StrategyPattern.xproj", "{01B9D869-AF89-4919-8445-79206848FB5F}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -64,6 +66,10 @@ Global
|
|||||||
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{01B9D869-AF89-4919-8445-79206848FB5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{01B9D869-AF89-4919-8445-79206848FB5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{01B9D869-AF89-4919-8445-79206848FB5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{01B9D869-AF89-4919-8445-79206848FB5F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -77,5 +83,6 @@ Global
|
|||||||
{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||||
{D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
{D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||||
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||||
|
{01B9D869-AF89-4919-8445-79206848FB5F} = {3820200F-354C-41E6-8F34-B301F5D621C2}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"projects": [ "src", "test" ],
|
"projects": [ "src", "test" ],
|
||||||
"sdk": {
|
"sdk": {
|
||||||
"version": "1.0.0-preview1-002702"
|
"version": "1.0.0-preview2-003121"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ using IteratorPattern;
|
|||||||
using MediatorPattern;
|
using MediatorPattern;
|
||||||
using MememntoPattern;
|
using MememntoPattern;
|
||||||
using ObserverPattern;
|
using ObserverPattern;
|
||||||
|
using StatePattern;
|
||||||
|
using StrategyPattern;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|||||||
@@ -17,7 +17,9 @@
|
|||||||
"type": "platform",
|
"type": "platform",
|
||||||
"version": "1.0.0-rc2-3002702"
|
"version": "1.0.0-rc2-3002702"
|
||||||
},
|
},
|
||||||
"ObserverPattern": "1.0.0-*"
|
"ObserverPattern": "1.0.0-*",
|
||||||
|
"StatePattern": "1.0.0-*",
|
||||||
|
"StrategyPattern": "1.0.0-*"
|
||||||
},
|
},
|
||||||
|
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
|
|||||||
19
src/StrategyPattern/Properties/AssemblyInfo.cs
Normal file
19
src/StrategyPattern/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("HP")]
|
||||||
|
[assembly: AssemblyProduct("StrategyPattern")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid("01b9d869-af89-4919-8445-79206848fb5f")]
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace StrategyPattern.ShippingCalculator
|
||||||
|
{
|
||||||
|
public class ShippingMotivatingExample
|
||||||
|
{
|
||||||
|
public static void Run()
|
||||||
|
{
|
||||||
|
MShippingCalculator shippingCostCaluclator = new MShippingCalculator();
|
||||||
|
var indiaPrice = shippingCostCaluclator.GetPrice(new MOrder(), MShippingService.IndiaPost);
|
||||||
|
var postaPrice = shippingCostCaluclator.GetPrice(new MOrder(), MShippingService.Posta);
|
||||||
|
var tcePrice = shippingCostCaluclator.GetPrice(new MOrder(), MShippingService.TCE);
|
||||||
|
|
||||||
|
Console.WriteLine("The shipping cost for india is:{0}, for posta is {1} for tce is {2}", indiaPrice, postaPrice, tcePrice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MShippingCalculator
|
||||||
|
{
|
||||||
|
public double GetPrice(MOrder order, MShippingService shippingService)
|
||||||
|
{
|
||||||
|
switch (shippingService)
|
||||||
|
{
|
||||||
|
case MShippingService.Posta:
|
||||||
|
return ComputeForPosta(order, shippingService);
|
||||||
|
case MShippingService.TCE:
|
||||||
|
return ComputeForTCE(order, shippingService);
|
||||||
|
case MShippingService.IndiaPost:
|
||||||
|
return ComputeForIndiaPost(order, shippingService);
|
||||||
|
default:
|
||||||
|
throw new Exception("Me not know the shipping service, boom boom");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private double ComputeForIndiaPost(MOrder order, MShippingService shippingService)
|
||||||
|
{
|
||||||
|
return 0.25;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double ComputeForTCE(MOrder order, MShippingService shippingService)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double ComputeForPosta(MOrder order, MShippingService shippingService)
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MOrder
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum MShippingService
|
||||||
|
{
|
||||||
|
Posta,
|
||||||
|
TCE,
|
||||||
|
IndiaPost
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace StrategyPattern.ShippingCalculator
|
||||||
|
{
|
||||||
|
public class ShippingWithStrategyExample
|
||||||
|
{
|
||||||
|
public static void Run()
|
||||||
|
{
|
||||||
|
ShippingCalculator shippingCostCaluclator = new ShippingCalculator();
|
||||||
|
var indiaPrice = shippingCostCaluclator.GetPrice(new Order(), new IndiaPost());
|
||||||
|
var postaPrice = shippingCostCaluclator.GetPrice(new Order(), new Posta());
|
||||||
|
var tcePrice = shippingCostCaluclator.GetPrice(new Order(), new TCE());
|
||||||
|
|
||||||
|
Console.WriteLine("The shipping cost for india is:{0}, for posta is {1} for tce is {2}", indiaPrice, postaPrice, tcePrice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ShippingCalculator
|
||||||
|
{
|
||||||
|
public double GetPrice(Order order, ShippingService shippingService)
|
||||||
|
{
|
||||||
|
return shippingService.GetPrice(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class ShippingService
|
||||||
|
{
|
||||||
|
public abstract double GetPrice(Order o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IndiaPost : ShippingService
|
||||||
|
{
|
||||||
|
public override double GetPrice(Order o)
|
||||||
|
{
|
||||||
|
return 0.25;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Posta : ShippingService
|
||||||
|
{
|
||||||
|
public override double GetPrice(Order o)
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TCE : ShippingService
|
||||||
|
{
|
||||||
|
public override double GetPrice(Order o)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Order
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/StrategyPattern/StrategyPattern.xproj
Normal file
21
src/StrategyPattern/StrategyPattern.xproj
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||||
|
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||||
|
<PropertyGroup Label="Globals">
|
||||||
|
<ProjectGuid>01b9d869-af89-4919-8445-79206848fb5f</ProjectGuid>
|
||||||
|
<RootNamespace>StrategyPattern</RootNamespace>
|
||||||
|
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
|
||||||
|
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||||
|
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||||
|
</Project>
|
||||||
18
src/StrategyPattern/StrategyPatternExamples.cs
Normal file
18
src/StrategyPattern/StrategyPatternExamples.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using StrategyPattern.ShippingCalculator;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace StrategyPattern
|
||||||
|
{
|
||||||
|
public class StrategyPatternExamples
|
||||||
|
{
|
||||||
|
public static void Run()
|
||||||
|
{
|
||||||
|
ShippingMotivatingExample.Run();
|
||||||
|
|
||||||
|
ShippingWithStrategyExample.Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/StrategyPattern/project.json
Normal file
13
src/StrategyPattern/project.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"version": "1.0.0-*",
|
||||||
|
|
||||||
|
"dependencies": {
|
||||||
|
"NETStandard.Library": "1.6.0"
|
||||||
|
},
|
||||||
|
|
||||||
|
"frameworks": {
|
||||||
|
"netstandard1.6": {
|
||||||
|
"imports": "dnxcore50"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user