1.抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strategy
{
abstract class Strategy
{
public abstract double computePrice(ProductOrder productOrder);
}
}
2.订单类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strategy
{
class ProductOrder
{
private double oldPrice;
private int userId;
private int productId;
public double OldPrice { get => oldPrice; set => oldPrice = value; }
public int UserId { get => userId; set => userId = value; }
public int ProductId { get => productId; set => productId = value; }
public ProductOrder(double oldPrice, int userId, int productId)
{
this.oldPrice = oldPrice;
this.userId = userId;
this.productId = productId;
}
}
}
3.无折扣策略
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strategy
{
class NormalActivity : Strategy
{
public override double computePrice(ProductOrder productOrder)
{
return productOrder.OldPrice;
}
}
}
4.折扣策略
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strategy
{
class DiscountActivity : Strategy
{
private double rate;
public DiscountActivity(double rate)
{
this.rate = rate;
}
public override double computePrice(ProductOrder productOrder)
{
return productOrder.OldPrice * rate;
}
}
}
5.优惠券策略
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strategy
{
class VoucherActivity : Strategy
{
private double voucher;
public VoucherActivity(double voucher)
{
this.voucher = voucher;
}
public override double computePrice(ProductOrder productOrder)
{
if(productOrder.OldPrice > voucher)
{
return productOrder.OldPrice - voucher;
}
else
{
return 0;
}
}
}
}
6.促销上下文
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strategy
{
class VoucherActivity : Strategy
{
private double voucher;
public VoucherActivity(double voucher)
{
this.voucher = voucher;
}
public override double computePrice(ProductOrder productOrder)
{
if(productOrder.OldPrice > voucher)
{
return productOrder.OldPrice - voucher;
}
else
{
return 0;
}
}
}
}
7.具体测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strategy
{
class Program
{
static void Main(string[] args)
{
ProductOrder productOrder = new ProductOrder(800, 001, 202);
Console.WriteLine("标准价格:800");
PromotionContext context;
context = new PromotionContext(new NormalActivity());
double finalPrice = context.ExecuteStrategy(productOrder);
Console.WriteLine("普通策略 价格:" + finalPrice);
context = new PromotionContext(new DiscountActivity(0.8));
finalPrice = context.ExecuteStrategy(productOrder);
Console.WriteLine("8折扣策略 价格:" + finalPrice);
context = new PromotionContext(new VoucherActivity(100));
finalPrice = context.ExecuteStrategy(productOrder);
Console.WriteLine("优惠券100策略 价格:" + finalPrice);
Console.ReadLine();
}
}
}