diff --git a/BehavioralPatterns.sln b/BehavioralPatterns.sln index c56791e..f91a289 100644 --- a/BehavioralPatterns.sln +++ b/BehavioralPatterns.sln @@ -26,6 +26,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ObserverPattern", "src\Obse EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StatePattern", "src\StatePattern\StatePattern.xproj", "{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StrategyPattern", "src\StrategyPattern\StrategyPattern.xproj", "{01B9D869-AF89-4919-8445-79206848FB5F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -77,5 +83,6 @@ Global {1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {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} + {01B9D869-AF89-4919-8445-79206848FB5F} = {3820200F-354C-41E6-8F34-B301F5D621C2} EndGlobalSection EndGlobal diff --git a/global.json b/global.json index b51e28b..e793049 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "projects": [ "src", "test" ], "sdk": { - "version": "1.0.0-preview1-002702" + "version": "1.0.0-preview2-003121" } } diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index 1aaa795..7a701ba 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -5,6 +5,8 @@ using IteratorPattern; using MediatorPattern; using MememntoPattern; using ObserverPattern; +using StatePattern; +using StrategyPattern; using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/src/BehavioralPatterns/project.json b/src/BehavioralPatterns/project.json index 3165efd..e614b94 100644 --- a/src/BehavioralPatterns/project.json +++ b/src/BehavioralPatterns/project.json @@ -17,7 +17,9 @@ "type": "platform", "version": "1.0.0-rc2-3002702" }, - "ObserverPattern": "1.0.0-*" + "ObserverPattern": "1.0.0-*", + "StatePattern": "1.0.0-*", + "StrategyPattern": "1.0.0-*" }, "frameworks": { diff --git a/src/StrategyPattern/Properties/AssemblyInfo.cs b/src/StrategyPattern/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bfbcda1 --- /dev/null +++ b/src/StrategyPattern/Properties/AssemblyInfo.cs @@ -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")] diff --git a/src/StrategyPattern/ShippingCalculator/ShippingMotivatingExample.cs b/src/StrategyPattern/ShippingCalculator/ShippingMotivatingExample.cs new file mode 100644 index 0000000..80437f5 --- /dev/null +++ b/src/StrategyPattern/ShippingCalculator/ShippingMotivatingExample.cs @@ -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 + } +} diff --git a/src/StrategyPattern/ShippingCalculator/ShippingWithStrategyExample.cs b/src/StrategyPattern/ShippingCalculator/ShippingWithStrategyExample.cs new file mode 100644 index 0000000..9b9b9b9 --- /dev/null +++ b/src/StrategyPattern/ShippingCalculator/ShippingWithStrategyExample.cs @@ -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 + { + } +} diff --git a/src/StrategyPattern/StrategyPattern.xproj b/src/StrategyPattern/StrategyPattern.xproj new file mode 100644 index 0000000..e960189 --- /dev/null +++ b/src/StrategyPattern/StrategyPattern.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + 01b9d869-af89-4919-8445-79206848fb5f + StrategyPattern + .\obj + .\bin\ + v4.5.2 + + + + 2.0 + + + diff --git a/src/StrategyPattern/StrategyPatternExamples.cs b/src/StrategyPattern/StrategyPatternExamples.cs new file mode 100644 index 0000000..3508d3a --- /dev/null +++ b/src/StrategyPattern/StrategyPatternExamples.cs @@ -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(); + } + } +} diff --git a/src/StrategyPattern/project.json b/src/StrategyPattern/project.json new file mode 100644 index 0000000..864b9a5 --- /dev/null +++ b/src/StrategyPattern/project.json @@ -0,0 +1,13 @@ +{ + "version": "1.0.0-*", + + "dependencies": { + "NETStandard.Library": "1.6.0" + }, + + "frameworks": { + "netstandard1.6": { + "imports": "dnxcore50" + } + } +}