Add strategy pattern basic example
This commit is contained in:
committed by
Petrutiu Mihai
parent
b5997ced99
commit
63c5ae3ab1
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