简单工厂和策略模式结合

商场促销活动:打折、满额返现等等

主函数对工厂模式和策略模式结合的调用:

    class Program
    {
        static void Main(string[] args)
        {
            //简单工厂模式
            CashSuper csuper = CashFactory.CreateCashAccept("打8折");
            float re = csuper.AcceptCash(100);

            //策略模式与简单工厂结合用法
            CashContext csup = new CashContext("打8折");
            float re1 = csup.GetResult(100);

            Console.WriteLine(re.ToString() +"----"+ re1.ToString());
            Console.ReadKey();
        }
    }

CashSuper类定义:(返回参加活动后结果,使用多态特性)

    public abstract class CashSuper
    {
        public abstract float AcceptCash(float money);
    }

正常价格、打折和满额返现的实现:

    public class CashNormal : CashSuper
    {
        public override float AcceptCash(float money)
        {
            return money;
        }
    }

    public class CashRebate : CashSuper
    {
        private float moneyRebate = 1;
        public CashRebate(string moneyRebate)
        {
            this.moneyRebate = float.Parse(moneyRebate);
        }
        public override float AcceptCash(float money)
        {
            return money * moneyRebate;
        }
    }

    public class CashReturn : CashSuper
    {
        private float moneyCondition = 0;
        private float moneyReturn = 0;
        public CashReturn(string moneyCondition, string moneyReturn)
        {
            this.moneyCondition = float.Parse(moneyCondition);
            this.moneyReturn = float.Parse(moneyReturn);
        }
        public override float AcceptCash(float money)
        {
            float result = money;
            if (money >= moneyCondition)
            {
                result = (float)(money - Math.Floor(money / moneyCondition) * moneyReturn);
            }
            return result;
        }
    }

简单工厂的工厂类:

    public class CashFactory
    {
        public static CashSuper CreateCashAccept(string type)
        {
            CashSuper cs = null;
            switch (type)
            {
                case "正常收费":
                    cs = new CashNormal();
                    break;
                case "满300返100":
                    cs = new CashReturn("300", "100");
                    break;
                case "打8折":
                    cs = new CashRebate("0.8");
                    break;
            }
            return cs;
        }
    }

策略模式结合简单工厂:

    class CashContext
    {
        private CashSuper cs;
        public CashContext(string type)
        {
            switch (type)
            {
                case "正常收费":
                    cs = new CashNormal();
                    break;
                case "满300返100":
                    cs = new CashReturn("300", "100");
                    break;
                case "打8折":
                    cs = new CashRebate("0.8");
                    break;
            }
        }
        public float GetResult(float money)
        {
            return cs.AcceptCash(money);
        }
    }

简单的代码,使用的面向对象的主要特性:封装、继承、多态。

原文地址:https://www.cnblogs.com/lmfeng/p/2594714.html