using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //装饰模式Demo namespace DisCountApplication2 { class Program { static void Main(string[] args) { //优惠方案一 Order order1 = new Order(); order1.GoodsTotal = 350; OrderDiscount orderDiscount = new OrderDiscount(); ApplyFullReductionDiscount d1 = new ApplyFullReductionDiscount(100, 10); ApplyDiscount d2 = new ApplyDiscount(0.95,200m); ApplyDiscount d3 = new ApplyDiscount(0.8); Console.WriteLine("\n优惠方案一"); d1.SetDiscountObject(orderDiscount); d2.SetDiscountObject(d1); d3.SetDiscountObject(d2); d3.Execute(order1); //优惠方案二 Order order2 = new Order(); order2.GoodsTotal = 150; OrderDiscount orderDiscount2 = new OrderDiscount(); ApplyFullReductionDiscount k1 = new ApplyFullReductionDiscount(100, 9.9m); ApplyDiscount k2 = new ApplyDiscount(0.98, 199m); ApplyDiscount k3 = new ApplyDiscount(0.88); Console.WriteLine("\n优惠方案二"); k1.SetDiscountObject(orderDiscount2); k3.SetDiscountObject(k1); k2.SetDiscountObject(k3); k2.Execute(order2); Console.ReadKey(); } } public class Order { /// <summary> /// 商品总价值 /// </summary> public decimal GoodsTotal{get;set;} /// <summary> /// 扣除折扣,最终客户要付款金额 /// </summary> public decimal GrandTotal{get;set;} } public interface IOrderDiscount { void Execute(Order order); } public class OrderDiscount : IOrderDiscount { public void Execute(Order order) { Console.WriteLine("原价为{0}", order.GoodsTotal.ToRoundString()); } } public abstract class DiscountBase : IOrderDiscount { private IOrderDiscount discount = null; public void SetDiscountObject(IOrderDiscount discount) { this.discount = discount; } public virtual void Execute(Order order) { if (discount != null) { discount.Execute(order); } } } //满减优惠 public class ApplyFullReductionDiscount : DiscountBase { private decimal fullMoney = decimal.Zero; private decimal reductionMoney = decimal.Zero; public ApplyFullReductionDiscount(decimal fullMoney,decimal reductionMoney) { this.fullMoney = fullMoney; this.reductionMoney = reductionMoney; } public override void Execute(Order order) { base.Execute(order); if (order.GoodsTotal >= fullMoney) { order.GoodsTotal = order.GoodsTotal - reductionMoney; Console.WriteLine("满{0}减{1}后的金额为{2}", this.fullMoney, this.reductionMoney, order.GoodsTotal.ToRoundString()); return; } Console.WriteLine("条件不满足,无法使用满{0}减{1}的优惠", this.fullMoney, this.reductionMoney); } } //打折优惠 public class ApplyDiscount : DiscountBase { private double discountRate = 1d; private decimal? moneyCondition = null; private Order order = null; public ApplyDiscount(double discountRate, decimal? moneyCondition) { this.discountRate = discountRate; this.moneyCondition = moneyCondition; } public ApplyDiscount(double discountRate) : this(discountRate,null) { } public override void Execute(Order order) { this.order = order; base.Execute(this.order); if (this.moneyCondition != null) { if (this.order.GoodsTotal >= this.moneyCondition.Value) { this.ExecuteDiscount(); } else { Console.WriteLine("条件不满足,金额未满{0},无法满足打{1}折的优惠",this.moneyCondition.Value.ToRoundString(), this.discountRate); return; } } else { this.ExecuteDiscount(); } Console.WriteLine("打{0}折后的金额为{1}", this.discountRate, order.GoodsTotal.ToRoundString()); } private void ExecuteDiscount() { this.order.GoodsTotal = this.order.GoodsTotal * (decimal)this.discountRate; } } //扩展Decimal类型 public static class DecimalExtend { public static string ToRoundString(this decimal d) { return Math.Round(d, 2, MidpointRounding.ToEven).ToString("f2"); } } }