# 设计模式之策略模式(商场打折)

Discount 类

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 策略者模式
{
public interface IDiscountStragety
{
double CalculateMoney(double totalMoney); // 定义折扣
}
//  打八折 (超过200元)
public class DiscountStrategyA : IDiscountStragety
{
public double CalculateMoney(double totalMoney)
{
return (totalMoney-200)>0? totalMoney* 0.8:totalMoney; //
}
}
// 满300 减50
public class DiscountStrategyB : IDiscountStragety
{
public double CalculateMoney(double totalMoney)
{
return (totalMoney - 300) > 0 ? (totalMoney - 50) : totalMoney;
}
}

// 满三百再减50 不满300打9折
public class DiscountStrategyC : IDiscountStragety
{
public double CalculateMoney(double totalMoney)
{
return (totalMoney - 300) > 0 ? (totalMoney - 50) : totalMoney * 0.9;
}
}
class Discount
{
private IDiscountStragety D_strategy;
public Discount(IDiscountStragety discount)
{
this.D_strategy = discount;
}
public double GetMoney(double totalMoney)
{
return D_strategy.CalculateMoney(totalMoney);
}
}
}

Main():

// 商场打折促销活动，策略1：打八折 策略:2： 满300减50 策略3：满300 减50元 不满300打9折
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 策略者模式
{
class Program
{
static void Main(string[] args)
{
Discount dd = new Discount(new DiscountStrategyA());
Console.WriteLine("应付款{0}",dd.GetMoney(330));
Console.WriteLine("------------------------------");
dd = new Discount(new DiscountStrategyB());
Console.WriteLine("应付款{0}", dd.GetMoney(330));
Console.WriteLine("------------------------------");
dd = new Discount(new DiscountStrategyC());
Console.WriteLine("应付款{0}", dd.GetMoney(290));
Console.WriteLine("------------------------------");