2、策略模式

一、一些基本定义和常识 Strategy['strætədʒɪ]n.

    策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。此模式让算法的变化,不会影响到使用算法的客户。

 二、策略模式的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.策略模式;

namespace Web._1策略模式
{
    /// <summary>
    /// Strategy类,定义所有支持的算法公共接口
    /// </summary>
    public class Strategy
    {
        /// <summary>
        /// 算法方法
        /// </summary>
        public abstract void AlgorithmInterface();//['ælɡərɪðəm] n. 运算法则;演算法;计算程序
    }

    /// <summary>
    /// 算法A
    /// </summary>
    internal class ConcreteStrategyA : Strategy
    {
        /// <summary>
        /// 算法A的实现
        /// </summary>
        public override void AlgorithmInterface()
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 算法B
    /// </summary>
    internal class ConcreteStrategyB : Strategy
    {
        /// <summary>
        /// 算法B的实现
        /// </summary>
        public override void AlgorithmInterface()
        {
            throw new NotImplementedException();
        }
    }

    internal class Context
    {
        private Strategy strategy;

        /// <summary>
        /// 初始化传入具体的策略对象
        /// </summary>
        /// <param name="strategy"></param>
        public Context(Strategy strategy)
        {
            this.strategy = strategy;
        }
        /// <summary>
        /// 上下文接口
        /// 根据具体的策略对象调用其算法的方法
        /// </summary>
        public void ContextInterface()
        {
            strategy.AlgorithmInterface();
        } 
    }

    /// <summary>
    /// 客户端
    /// 
    /// </summary>
    class Customer
    { 
        private static void Main(string[] args)
        {
            Context context;

            //由于实例化不同的策略,所以最终调用的ContextInterface()结果也是不同的
            context=new Context(new ConcreteStrategyA());
            context.ContextInterface();

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

 三、简单工厂问题的解决

 

internal class CashContext
    {
        private CashSuper cs;

        public CashContext(string type)
        {
            switch (type)
            {
                case "正常收费":
                    CashNoraml cash0 = new CashNoraml();
                    cs = cash0;
                    break;
                case "满300返100":
                    CashReturn cash1 = new CashReturn("300", "100");
                    cs = cash1;
                    break;
                case "打折:85折":
                    CashRebate cash2 = new CashRebate("0.85");
                    cs = cash2;
                    break; 
            }
        }

        public double GetResult(double money)
        {
            return cs.AcceptCash(money);
        }
    }
    /// <summary>
    /// 客户端
    /// </summary>
    internal class Customer
    {
        public void Method()
        {
            CashContext cc = new CashContext("type");
            cc.GetResult(100d);
        }
    }

二者的区别:

简单工厂模式

 /// <summary>
    /// 客户端实现
    /// </summary>
    class Customer
    {
        public void Method()
        {
             CashSuper cash = CashFactory.CreateCashSuper("type");
             cash.AcceptCash(100);
        } 
    }

策略模式和简单工厂模式

/// <summary>
    /// 客户端
    /// </summary>
    internal class Customer
    {
        public void Method()
        {
            CashContext cc = new CashContext("type");
            cc.GetResult(100d);
        }
    }

简单工厂需识别两个类:CashSuper、CashFatory而策略模式和简单工厂结合的方法只有CashContext,降低了耦合度。

总结:

1、策略模式的Strategy类层次为Context定义了一系列的可供重用的算法和行为,继承有助于吸取出这些算法的公共功能。
2、策略模式的优点简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。
3、在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象


作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/PEPE/p/3461043.html