1

i am learning patterns and i got confused when i came across state and startegy pattern. A simple googling took me to this stact overflow blog which made to get confused even more.

My simple quesiton is,

strategy pattern

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();
        }
    }

state pattern

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();
        }
    }

in the above example, as per my understanding, i would take this as, passing a object to the context without any state saved is the strategy pattern and keeping the sate in the context is the state pattern.

is my understanding right?

UPDATE 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();
        }
    }

so as per my understanding, is this approach a state pattern and the approach below a strategy pattern?

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();
        }
    }

please let me know, thank you

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85

1 Answers1

2

This technical difference is not wrong but I think that you don't use the correct approach to capture the difference between these two patterns.

State and strategy are classed as behavioral design patterns.
Which brings them together but conceptually these are really two distinct things.

In your example and generally speaking, choosing an algorithm type is a way of proceeding and should be so considered as a strategy.
Trying to convert it into a state makes no sense.
A state pattern allows an object to alter its behavior when its internal state changes.
And your algorithms don't have any internal state changes.
These are distinct strategies. And your prove it as you never change from a state to another.
Trying to understand the difference between the two design patterns with the same use case can only finish as trying to put a circle in a square and does not allow a good understanding.

If you want really to stay in the algorithm domain, you could use a related use case.
For example in a family of algorithms, you can have simple of them (as which one defined in your question) and more complex that contain an internal state and change their behavior according to this state.
The state pattern could be useful to implement these algorithms.

But you can also rely on a completely different example to understand the state pattern.
Here is a question about state pattern I answered where the matter is modeling the washing machine, a good candidate to understand the state pattern.

davidxxx
  • 125,838
  • 23
  • 214
  • 215