【问题标题】:Simplified difference between Strategy and State pattern策略和状态模式之间的简化区别
【发布时间】:2018-06-28 19:04:27
【问题描述】:

我正在学习模式,当我遇到状态和策略模式时我感到很困惑。一个简单的谷歌搜索把我带到了这个stact overflow blog,这让我更加困惑。

我的简单问题是,

战略模式

public interface Istrategy {
        void algorithm();
    }
    public class add : Istrategy
    {
        public void algorithm()
        {
            Console.WriteLine("addition");
        }
    }
    public class multiply : Istrategy
    {
        public void algorithm()
        {
            Console.WriteLine("multiply");
        }
    }
    public class subtract : Istrategy
    {
        public void algorithm()
        {
            Console.WriteLine("subtract");
        }
    }
    public class context {
        public context(Istrategy strategy) {
            strategy.algorithm();
        }
    }
    public class client {
        static void Main() {
            new context(new add());

            Console.ReadLine();
        }
    }

状态模式

public interface Istate {
        void algorithm();
    }
    public class add : Istate
    {
        public void algorithm()
        {
            Console.WriteLine("addition");
        }
    }
    public class multiply : Istate
    {
        public void algorithm()
        {
            Console.WriteLine("multiply");
        }
    }
    public class subtract : Istate
    {
        public void algorithm()
        {
            Console.WriteLine("subtract");
        }
    }
    public class context {
        Istate state;
        public context(Istate state) {
            state.algorithm();
        }
    }
    public class client {
        static void Main() {
            new context(new add());

            Console.ReadLine();
        }
    }

在上面的例子中,根据我的理解,我认为将对象传递给上下文而不保存任何状态是策略模式,而将状态保持在上下文中是状态模式。

我的理解对吗?

更新 1

interface Istate
    {
        void action();
    }
    class walk : Istate
    {
        public void action()
        {
            Console.WriteLine("walking !!!");
        }
    }
    class run : Istate
    {
        public void action()
        {
            Console.WriteLine("running !!!");
        }
    }
    class fly : Istate
    {
        public void action()
        {
            Console.WriteLine("flying !!!");
        }
    }
    class context
    {
        Istate state;
        public void statePattern()
        {
            for (var i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    state = new walk();
                    state.action();
                }
                if (i == 1)
                {
                    state = new run();
                    state.action();
                }
                if (i == 2)
                {
                    state = new fly();
                    state.action();
                }
            }
        }
    }
    public class client
    {
        static void Main()
        {
            new context().statePattern();

            Console.ReadLine();
        }
    }

所以根据我的理解,这种方法是状态模式还是策略模式下的方法?

interface Istate
    {
        void action();
    }
    class walk : Istate
    {
        public void action()
        {
            Console.WriteLine("walking !!!");
        }
    }
    class run : Istate
    {
        public void action()
        {
            Console.WriteLine("running !!!");
        }
    }
    class fly : Istate
    {
        public void action()
        {
            Console.WriteLine("flying !!!");
        }
    }
    class context
    {
        public void statePattern(Istate state)
        {
            state.action();
        }
        public void startegy()
        {
            for (var i = 0; i < 3; i++)
            {
                if (i == 0)
                {
                    statePattern(new walk());
                }
                if (i == 1)
                {
                    statePattern(new run());
                }
                if (i == 2)
                {
                    statePattern(new fly());
                }
            }
        }
    }
    public class client
    {
        static void Main()
        {
            new context().startegy();

            Console.ReadLine();
        }
    }

请告诉我,谢谢

【问题讨论】:

    标签: design-patterns strategy-pattern state-pattern


    【解决方案1】:

    这种技术差异并没有错,但我认为您没有使用正确的方法来捕捉这两种模式之间的差异。

    状态和策略被归类为行为设计模式。
    这将它们结合在一起,但从概念上讲,它们实际上是两个截然不同的东西。

    在您的示例中,一般来说,选择算法类型是一种处理方式,应该被视为一种策略。
    试图将其转换为状态是没有意义的。
    状态模式允许对象在其内部状态发生变化时改变其行为。
    而且您的算法没有任何内部状态更改。
    这些是不同的策略。并且您证明了这一点,因为您永远不会从一个状态变为另一个状态。
    试图用相同的用例来理解两种设计模式之间的区别只能以试图将一个圆放在一个正方形中而告终,并不能很好地理解。

    如果您真的想留在算法领域,您可以使用相关的用例。
    例如,在一系列算法中,您可以拥有简单的算法(如您的问题中定义的那样)和更复杂的算法,其中包含内部状态并根据此状态更改其行为。
    状态模式可能有助于实现这些算法。

    但您也可以依靠完全不同的示例来理解状态模式。
    这是a question关于状态模式我回答的问题是在洗衣机建模,了解状态模式的好人选。

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 2010-12-12
      • 2013-01-18
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      相关资源
      最近更新 更多