C#设计模式之:策略模式

需求:做一个商场收银软件,营业员根据客户所购买商品的单价和数量,向客户收费。
额外需求1:打折(8折,7折,5折不等)
额外需求2:返现(满300返100,满200返50等)

使用简单工厂实现

UML类图

这里写图片描述

代码

namespace PatternTest.Strategy
{
    abstract class CashSuper
    {
        public abstract double acceptCash(double money);
    }
}
namespace PatternTest.Strategy
{
    class CashNormal : CashSuper
    {
        public override double acceptCash(double money)
        {
            return money;
        }
    }
}
namespace PatternTest.Strategy
{
    class CashRebate : CashSuper
    {
        private double moneyRebate = 1d;

        public CashRebate(string moneyRebate)
        {
            this.moneyRebate = double.Parse(moneyRebate);
        }

        public override double acceptCash(double money)
        {
            return money * moneyRebate;
        }
    }
}
namespace PatternTest.Strategy
{
    class CashReturn : CashSuper
    {
        private double moneyCondition = 0.0d;
        private double moneyReturn = 0.0d;

        public CashReturn(string moneyCondition, string moneyReturn)
        {
            this.moneyCondition = double.Parse(moneyCondition);
            this.moneyReturn = double.Parse(moneyReturn);
        }

        public override double acceptCash(double money)
        {
            double result = money;
            if (money >= moneyCondition)
                result = money - Math.Floor(money / moneyCondition) * moneyReturn;

            return result;
        }
    }
}
namespace PatternTest.Strategy
{
    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;
        }

    }
}
//test
double total = 0.0d;
CashSuper cs = CashFactory.createCashAccept("满300返100");
double totalPrices = 0d;
totalPrices = cs.acceptCash(100.00 * 3);
total += totalPrices;

cs = CashFactory.createCashAccept("打8折");
totalPrices = cs.acceptCash(100.00 * 2);
total += totalPrices;

Console.WriteLine("结果是: " + total);

结果

结果是: 360
请按任意键继续. . .

使用策略模式重写

策略模式模型

这里写图片描述

Strategy:
策略类,定义所有支持的算法公共接口

ConcreteStrategyA、ConcreteStrategyB、ConcreteStrategyC等:
具体的策略类,封装了算法的具体算法或行为,继承于Strategy

Context:
用ConcreteStrategy来配置,具体使用其基类的指针或引用表示

扩展:
可以让Context为基类,扩展其子类,代码更加的灵活

代码:
// Strategy.cs
namespace PatternTest.Strategy
{
    abstract class Strategy
    {
        public abstract void AlgorithmInterface();
    }
}
// ConcreteStrategyA.cs 
namespace PatternTest.Strategy
{
    class ConcreteStrategyA : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法A实现");
        }
    }
}
// ConcreteStrategyB.cs
namespace PatternTest.Strategy
{
    class ConcreteStrategyB : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法B实现");
        }
    }
}
// ConcreteStrategyC.cs
namespace PatternTest.Strategy
{
    class ConcreteStrategyC : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法C实现");
        }
    }
}
// Context.cs
namespace PatternTest.Strategy
{
    class Context
    {
        Strategy strategy;

        public Context(Strategy strategy)
        {
            this.strategy = strategy;
        }

        public void ContextInterface()
        {
            strategy.AlgorithmInterface();
        }

    }
}
//test
Context context;
context = new Context(new ConcreteStrategyA());
context.ContextInterface();

context = new Context(new ConcreteStrategyB());
context.ContextInterface();

context = new Context(new ConcreteStrategyC());
context.ContextInterface();
// 输出结果
算法A实现
算法B实现
算法C实现
请按任意键继续. . .
套用策略模型到简单工厂里

Strategy => CashSuper
ConcreteStrategyA => CashNormal
ConcreteStrategyB => CashRebate
ConcreteStrategyC => CashReturn
Context => CashContext (新增)

UML类图
这里写图片描述

前面 几个类代码是一样的,只有CashContext.cs是新增的

namespace PatternTest.Strategy
{
    class CashContext
    {
        CashSuper cs = null;

        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 double GetResult(double money)
        {
            return cs.acceptCash(money);
        }
    }
}
double total = 0.0d;
CashContext cc = new CashContext("满300返100");
double totalPrices = 0d;
totalPrices = cc.GetResult(300);
total += totalPrices;
Console.WriteLine("结果是: " + total);

cc = new CashContext("打8折");
totalPrices = cc.GetResult(100.00 * 2);
total += totalPrices;
Console.WriteLine("结果是: " + total);
// 输出结果
结果是: 200
结果是: 360
请按任意键继续. . .

简单工厂模式:客户端会认识两个类CashSuperCashFactory
策略模式结合简单工厂模式:客户端只认识一个类CashContext

策略模式优点:

1,以相同的方式调用不同的算法,减少了各种算法与使用算法类之间的耦合
2,简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试

缺陷:

CashContext中还是用到了 switch,也就是说如果我们要新增加一个算法,就必须要更改CashContext中的switch分支代码
更好的办法是使用反射

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值