【c#】机房重构之策略模式

What?

它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法变化,不影响使用算法的客户。

How?

【c#】机房重构之策略模式_第1张图片

Where?

此设计模式我用在了计算金额的地方,由于机房重构中有两种卡(会员卡和非会员卡),因此使用策略模式进行计算,下面是在下机中的使用。小编使用的是策略模式+简单工厂

代码展示

1、策略模式:

        /// 
        /// 承接上下
        /// 
        /// 
        public class Cashcontext
        {
            CashSuper cs = null;

            public Cashcontext(Entity.CardInfo CardInfo)
            {

                switch (CardInfo.CardType)
                {
                    case "会员卡":
                        Rate rate = new Rate();
                        cs = rate;
                        break;
                    case "非会员卡":
                        TmpRate tmprate = new TmpRate();
                        cs = tmprate;
                        break;
                }

            }
            public Entity.LineInfo GetResult(Entity.LineInfo LineInfo, Entity.BasicDataInfo BasicDataInfo)
            {
                return cs.acceptCash(LineInfo, BasicDataInfo);
            }
        }
        /// 
        /// 抽象类
        /// 
        public abstract class CashSuper
        {
            //算法方法
            public abstract Entity.LineInfo acceptCash(Entity.LineInfo LineInfo, Entity.BasicDataInfo BasicDataInfo);
        }
        /// 
        /// 会员
        /// 
        public class Rate : CashSuper
        {
            public override Entity.LineInfo acceptCash(Entity.LineInfo LineInfo, Entity.BasicDataInfo BasicDataInfo)
            {
                
                //计算花费金额
                if (Convert.ToInt32(BasicDataInfo.UnitTime) != 0)
                {
                    if (LineInfo.ConsumeTime % Convert.ToInt32(BasicDataInfo.UnitTime) == 0)
                    {
                        LineInfo.ConsumCash = (LineInfo.ConsumeTime / Convert.ToInt32(BasicDataInfo.UnitTime)) * BasicDataInfo.Rate;
                    }
                    else
                    {
                        LineInfo.ConsumCash = (LineInfo.ConsumeTime / Convert.ToInt32(BasicDataInfo.UnitTime) + 1) * BasicDataInfo.Rate;
                    }
                }
                
                return LineInfo;
            }


        }
        /// 
        /// 非会员
        /// 
        public class TmpRate : CashSuper
        {
            public override Entity.LineInfo acceptCash(Entity.LineInfo LineInfo, Entity.BasicDataInfo BasicDataInfo)
            {
                //计算花费金额
                if (LineInfo.ConsumeTime % Convert.ToInt32(BasicDataInfo.UnitTime) == 0)
                {
                    LineInfo.ConsumCash = (LineInfo.ConsumeTime / Convert.ToInt32(BasicDataInfo.UnitTime)) * BasicDataInfo.TmpRate;
                }
                else
                {
                    LineInfo.ConsumCash = (LineInfo.ConsumeTime / Convert.ToInt32(BasicDataInfo.UnitTime) + 1) * BasicDataInfo.TmpRate;
                }
                return LineInfo;
            }

        }

2、B层调用

public Entity.LineInfo SelectCash(Entity.LineInfo LineInfo, Entity.CardInfo CardInfo, Entity.BasicDataInfo BasicDataInfo)
        {
            Strategy Strategy = new Strategy();
            Strategy.Cashcontext cashContext = new Strategy.Cashcontext(CardInfo);
            Entity.LineInfo lineInfo = cashContext.GetResult(LineInfo, BasicDataInfo);
            return lineInfo;
        }

你可能感兴趣的:(C#,c#成长之路)