软件设计模式之策略模式(Strategy)(Strategy of software design pattern)

描述

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

实现

    /*
     策略模式
     */

    /// <summary>
    /// 策略模式抽象算法类
    /// </summary>
    abstract class Strategy
    {
        public abstract void AlgorithmInterface();

    }

    /// <summary>
    /// 策略模式实现类A
    /// </summary>
    class ConcreteStrategyA : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法A实现");
        }
    }

    /// <summary>
    /// 策略模式实现类B
    /// </summary>
    class ConcreteStrategyB : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法B实现");
        }
    }

    /// <summary>
    /// 策略模式实现类C
    /// </summary>
    class ConcreteStrategyC : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法C实现");
        }
    }

    /// <summary>
    /// 上下文接口
    /// </summary>
    class Context
    {
        Strategy _strategy;
        /// <summary>
        /// 初始化时传入策略对象
        /// </summary>
        /// <param name="strategy"></param>
        public Context(Strategy strategy)
        {
            _strategy = strategy;
        }

        /// <summary>
        /// 调用算法
        /// </summary>
        public void ContextInterface()
        {
            _strategy.AlgorithmInterface();
        }
    }

与简单工厂差别

工厂模式是创建型模式,适应对象的变化。策略模式是行为性模式,适应行为的变化。

策略模式、简单工厂适用场景

我认为工厂模式适合的场景是不复杂的,比较具体的,比如文具:铅笔、尺子、橡皮;

策略模式则是适用于算法的,比如商场折扣:买200反20,买300反30;

————————

describe

It defines the algorithm family and encapsulates them separately so that they can replace each other. This mode makes the change of the algorithm not affect the customers using the algorithm.

realization

    /*
     策略模式
     */

    /// <summary>
    /// 策略模式抽象算法类
    /// </summary>
    abstract class Strategy
    {
        public abstract void AlgorithmInterface();

    }

    /// <summary>
    /// 策略模式实现类A
    /// </summary>
    class ConcreteStrategyA : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法A实现");
        }
    }

    /// <summary>
    /// 策略模式实现类B
    /// </summary>
    class ConcreteStrategyB : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法B实现");
        }
    }

    /// <summary>
    /// 策略模式实现类C
    /// </summary>
    class ConcreteStrategyC : Strategy
    {
        public override void AlgorithmInterface()
        {
            Console.WriteLine("算法C实现");
        }
    }

    /// <summary>
    /// 上下文接口
    /// </summary>
    class Context
    {
        Strategy _strategy;
        /// <summary>
        /// 初始化时传入策略对象
        /// </summary>
        /// <param name="strategy"></param>
        public Context(Strategy strategy)
        {
            _strategy = strategy;
        }

        /// <summary>
        /// 调用算法
        /// </summary>
        public void ContextInterface()
        {
            _strategy.AlgorithmInterface();
        }
    }

Difference from simple factory

Factory mode is a creation mode, which adapts to the changes of objects. Strategy model is a behavioral model, which adapts to the changes of behavior.

Strategy mode, simple factory application scenario

I think the factory model is suitable for the scenes that are not complex and more specific, such as stationery: pencil, ruler and eraser;

The strategy mode is applicable to the algorithm, such as mall discount: buy 200 against 20 and buy 300 against 30;