zoukankan      html  css  js  c++  java
  • 简单工厂和策略模式结合

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

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

        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);
            }
        }

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

  • 相关阅读:
    通过actionlib控制jaco机械臂
    actionlib学习
    配置 jaco机械臂 ros环境
    ubuntu14.04标题栏显示上下网速
    linux下alsa架构音频驱动播放wav格式文件
    ros语音交互(五)移植科大讯飞语音识别到ros
    ubuntu14.04 wifi驱动
    Ubuntu14.04使用apt-fast来加快apt-get下载的教程
    ROS语音交互(四)接入图灵语义理解
    相比传统游戏,区块链游戏的价值在哪里?
  • 原文地址:https://www.cnblogs.com/lmfeng/p/2594714.html
Copyright © 2011-2022 走看看