From 474a923c60e5de90fe67cb124d2dd0114ef35755 Mon Sep 17 00:00:00 2001 From: Petrutiu Mihai Date: Sat, 2 Jul 2016 15:15:38 +0300 Subject: [PATCH] Strategy pattern shipping example --- .../ShippingMotivatingExample.cs | 6 ++-- .../ShippingWithStrategyExample.cs | 35 +++++++++++++++---- .../StrategyPatternExamples.cs | 3 ++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/StrategyPattern/ShippingCalculator/ShippingMotivatingExample.cs b/src/StrategyPattern/ShippingCalculator/ShippingMotivatingExample.cs index 80437f5..a048904 100644 --- a/src/StrategyPattern/ShippingCalculator/ShippingMotivatingExample.cs +++ b/src/StrategyPattern/ShippingCalculator/ShippingMotivatingExample.cs @@ -37,17 +37,17 @@ namespace StrategyPattern.ShippingCalculator private double ComputeForIndiaPost(MOrder order, MShippingService shippingService) { - return 0.25; + return 50; } private double ComputeForTCE(MOrder order, MShippingService shippingService) { - return 1; + return 150; } private double ComputeForPosta(MOrder order, MShippingService shippingService) { - return 2; + return 200; } } diff --git a/src/StrategyPattern/ShippingCalculator/ShippingWithStrategyExample.cs b/src/StrategyPattern/ShippingCalculator/ShippingWithStrategyExample.cs index 9b9b9b9..b76800c 100644 --- a/src/StrategyPattern/ShippingCalculator/ShippingWithStrategyExample.cs +++ b/src/StrategyPattern/ShippingCalculator/ShippingWithStrategyExample.cs @@ -10,11 +10,19 @@ namespace StrategyPattern.ShippingCalculator 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()); + var order = new Order(); + var indiaPrice = shippingCostCaluclator.GetPrice(order, new IndiaPost()); + var postaPrice = shippingCostCaluclator.GetPrice(order, new Posta()); + var tcePrice = shippingCostCaluclator.GetPrice(order, new TCE()); - Console.WriteLine("The shipping cost for india is:{0}, for posta is {1} for tce is {2}", indiaPrice, postaPrice, tcePrice); + var priceOfTrainConductor = shippingCostCaluclator.GetPrice(order, new SimpleService(o => 10)); + + const int priceOfBeer = 5; + var prifeOfIon = shippingCostCaluclator.GetPrice(order, new SimpleService(o => priceOfBeer)); + + Console.WriteLine(@"The shipping cost for india is:{0}, for posta is {1} for tce is {2}, +if you talk to train conductor:{3}, if you go to Ion {4}", indiaPrice, postaPrice, + tcePrice, priceOfTrainConductor, prifeOfIon); } } @@ -35,7 +43,7 @@ namespace StrategyPattern.ShippingCalculator { public override double GetPrice(Order o) { - return 0.25; + return 50; } } @@ -43,7 +51,7 @@ namespace StrategyPattern.ShippingCalculator { public override double GetPrice(Order o) { - return 2; + return 200; } } @@ -51,7 +59,20 @@ namespace StrategyPattern.ShippingCalculator { public override double GetPrice(Order o) { - return 1; + return 150; + } + } + + public class SimpleService : ShippingService + { + Func simpleCalculator; + public SimpleService(Func simpleCalculator) + { + this.simpleCalculator = simpleCalculator; + } + public override double GetPrice(Order o) + { + return simpleCalculator(o); } } diff --git a/src/StrategyPattern/StrategyPatternExamples.cs b/src/StrategyPattern/StrategyPatternExamples.cs index 3508d3a..59a2b6a 100644 --- a/src/StrategyPattern/StrategyPatternExamples.cs +++ b/src/StrategyPattern/StrategyPatternExamples.cs @@ -13,6 +13,9 @@ namespace StrategyPattern ShippingMotivatingExample.Run(); ShippingWithStrategyExample.Run(); + + //TODO: use actions/funcs, add more examples + //Show the switch moving problem } } }