Add strategy pattern basic example
This commit is contained in:
committed by
Petrutiu Mihai
parent
b5997ced99
commit
63c5ae3ab1
@@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user