コード例 #1
0
        public static CashSuper CreateCashAccept(string type)
        {
            CashSuper cs;

            switch (type)
            {
            case "满300返100":
                cs = new CashReturn("300", "100");
                break;

            case "打8折":
                cs = new CashRebate("0.8");
                break;

            case "打7折":
                cs = new CashRebate("0.7");
                break;

            case "打5折":
                cs = new CashRebate("0.5");
                break;

            default:
                cs = new CashNormal();
                break;
            }

            return(cs);
        }
コード例 #2
0
        /// <summary>
        /// 【策略模式】Strategy 行為型模式
        /// <para>
        /// 定義了算法家族,分別封裝起來,讓彼此之間可以互相替換
        /// 此模式讓算法的變化,不會影響到使用算法的客戶
        /// </para>
        /// </summary>
        public void ExecuteStrategyMode()
        {
            CashSuper strage1 = new CashNormal();//正常狀況

            Console.WriteLine($"Class Type => {strage1.GetType()} 金額 => {50} 策略運算 => {strage1.AcceptCashResult(50)}");
            Console.WriteLine($"Class Type => {strage1.GetType()} 金額 => {20} 策略運算 => {strage1.AcceptCashResult(20)}");

            strage1 = new CashRebate("0.8");//打8折
            Console.WriteLine($"Class Type => {strage1.GetType()} 金額 => {50} 策略運算 => {strage1.AcceptCashResult(50)}");
            Console.WriteLine($"Class Type => {strage1.GetType()} 金額 => {20} 策略運算 => {strage1.AcceptCashResult(20)}");

            strage1 = new CashReturn("15", "5");//消費15 元 返金5元
            Console.WriteLine($"Class Type => {strage1.GetType()} 金額 => {50} 策略運算 => {strage1.AcceptCashResult(50)}");
            Console.WriteLine($"Class Type => {strage1.GetType()} 金額 => {20} 策略運算 => {strage1.AcceptCashResult(20)}");
        }
コード例 #3
0
 public static CashSuper createCashAccept(string type)
 {
     CashSuper cs = null;
     switch (type)
     {
         case "正常收费":
             cs = new CashNormal();
             break;
         case "满300返100":
             CashReturn cr1 = new CashReturn("300", "100");
             cs = cr1;
             break;
         case "打8折":
             CashRebate cr2 = new CashRebate("0.8");
             cs = cr2;
             break;
     }
     return cs;
 }
コード例 #4
0
        //根据条件返回相应的对象
        public CashContext(string type)
        {
            switch (type)
            {
            case "正常收费":
                CashNormal cs0 = new CashNormal();
                cs = cs0;
                break;

            case "满300返100":
                CashReturn cr1 = new CashReturn("300", "100");
                cs = cr1;
                break;

            case "打8折":
                CashRebate cr2 = new CashRebate("0.8");
                cs = cr2;
                break;
            }
        }
コード例 #5
0
    public static CashSuper createCashAccept(string type)
    {
        CashSuper cs = null;

        switch (type)
        {
        case "Normal":
            cs = new CashNormal();
            break;

        case "300 Return 100":
            cs = new CashReturn("300", "100");
            break;

        case "80% discount":
            cs = new CashRebate("0.8");
            break;
        }
        return(cs);
    }
コード例 #6
0
    public static CashSuper CreateCashAccept(string type)
    {
        CashSuper cs = null;

        switch (type)
        {
        case "正常收费":
            cs = new CashNormal();
            break;

        case "满300减100":
            CashReturn cashReturn = new CashReturn("300", "100");
            cs = cashReturn;
            break;

        case "打八折":
            CashRebate cashRebate = new CashRebate("0.8");
            cs = cashRebate;
            break;
        }
        return(cs);
    }
コード例 #7
0
        //根据条件返回相应的对象
        public static CashSuper createCashAccept(string type)
        {
            CashSuper cs = null;

            switch (type)
            {
            case "正常收费":
                cs = new CashNormal();
                break;

            case "满300返100":
                CashReturn cr1 = new CashReturn("300", "100");
                cs = cr1;
                break;

            case "打8折":
                CashRebate cr2 = new CashRebate("0.8");
                cs = cr2;
                break;
            }
            return(cs);
        }
コード例 #8
0
        public CashContext(string type)
        {
            switch (type)
            {
            case "Normal charge":
                CashNormal c1 = new CashNormal();
                _cashCharge = c1;
                break;

            case "300 minus 100":
                CashReturn c2 = new CashReturn("300", "100");
                _cashCharge = c2;
                break;

            case "20% off":
                CashRebate c3 = new CashRebate("0.8");
                _cashCharge = c3;
                break;

            default:
                throw new Exception("error");
            }
        }
コード例 #9
0
ファイル: CashFactory.cs プロジェクト: USuperMe/StrategyModel
        public static CashBase CreateCashAccept(string type)
        {
            CashBase cs = null;

            switch (type)
            {
            case "正常收费":
                cs = new CashNormal();
                break;

            case "满300返100收费":
                cs = new CashReturn(300, 100);
                break;

            case "打折收费":
                cs = new CashRebate(0.8f);
                break;

            default:
                break;
            }
            return(cs);
        }
コード例 #10
0
        /// <summary>
        /// 收费对象生成工厂
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static CashSuper CreateCashAccecpt(string type)
        {
            CashSuper cashSuper = null;

            switch (type)
            {
            case "正常收费":
                cashSuper = new CashNormal();
                break;

            case "打8折":
                cashSuper = new CashRebate("0.8");
                break;

            case "满200减10":
                cashSuper = new CashReturn("200", "10");
                break;

            default:
                break;
            }
            return(cashSuper);
        }
コード例 #11
0
    public static CashSuper createCashAccept(string type)
    {
        CashSuper cs = null;

        switch(type){
            case "Normal":
                cs = new CashNormal();
                break;
            case "300 Return 100":
                cs = new CashReturn("300", "100");
                break;
            case "80% discount":
                cs = new CashRebate("0.8");
                break;
        }
        return cs;
    }
コード例 #12
0
        public void acceptCashTest(double money)
        {
            CashNormal normal = new CashNormal();

            Assert.Equal(normal.acceptCash(money), money);
        }