Five ‘s Weblog

October 5, 2007

The differences between Strategy pattern and State pattern

Filed under: Five's thought — by powerdream5 @ 2:54 am
Tags: , ,


       Recently, I am reading some articles about Refactoring, and I have draw two conclusions. The first one is that one of the benefits of refactoring is to change the procedure design into object-oriented design. The second one is that the design patterns really play an important role in the process of refactoring. For example, one of the refactoring ways is replacing conditions with polymorphism. In this case, strategy pattern and state pattern are the key!
       However, some programmers are very confused with strategy pattern and state pattern, because ther are pretty similar to each other. So today, I want to show the differences between them in my mind!

        Firstly, Let me give the definition of the two pattern according to the classic book – <<Design Patterns: Elements of Reusable Object-Oriented Software>>
        Strategy pattern: Define a family of algorithms, encapsulate each one and make them interchangeable.
        State pattern: Allow an object to alert its behavior when its internal state changes. The object will appear to change its class.

        perhaps you would have a headache just looking at the definitions, but I hope you can have a deep thought of them after reading this article.
        In order to distinguish the two patterns, we have to answer the question that why they look similar to each other. The answer is that the main concept of the two patterns is polymorphism. Both of them define a parent class and a series of children class inherit the parent class. And the client encapsulate the call to the method of the parent class (in fact, the client calls the method of the children class).
        In my opinion, the biggest difference between strategy pattern and state pattern is that we encapsulate each algorithm into a class in strategy pattern, but we encapsulate each state into a class in state pattern. So before deciding to use strategy pattern or state pattern to solve a problem, we must have the idea that what is the key point of the problem. is it the algorithm or is it the state? Now, let’s give an example to show it more clearly. 

Image from naturetrek

        In a zoo, a feeder is in charge of feeding a tiger. the tiger has three states, hungry, medium and full. Now, there are two cases.
        The first one is the feeder feed the tiger different kinds of food according to the state of the tiger. For instance, when the tiger is hungry, the feeder should support it meat; when the tiger is in medium, the feeder should support it cake; when the tiger is full, the feeder should support it water.
        The second one is each time the feeder will provide the tiger meat, cake and water. However, the tiger will eat only one of the three food according to the state of itself. When the tiger is hungry, it eats meat; when the tiger is in medium, it eats cake; when the tiger is full, it drinks water.
        After comparing the two cases, we know we need to realize the first case using strategy pattern and realize the second case using state pattern. So, now you can program by yourself, which will help you make more sense, or you can download the source files I made.

        In the former example, we think the state of the tiger doesn’t change. Now, let’s make something different. If the state of the tiger will change, what is going to happen. For example, at first, the tiger is hungry, and the feeder feed it meat or it eats meat, then the tiger transfer to the second state – medium, so the feeder should feet it cake or it eats cake. Therefore, how can we make changes to the programs we made for the former example. I hope you make a conclusion that it will be easier to change the program realized by state pattern than the one realized by strategy pattern in this case.

       After reading the words above, I hope you have a good understanding of the differences between strategy pattern and state pattern. If you have any different opinions or questions, welcome to leave message to me!


  1. good but hard

    Comment by chen — October 5, 2007 @ 3:05 am |Reply

  2. what’s the hell is that? 🙂

    Comment by Visa — October 5, 2007 @ 3:08 am |Reply

  3. You make the complex relationship between Strategy pattern and State pattern easy through this story. It is amazing and you are so smart.

    Comment by Yuan — October 8, 2007 @ 5:24 am |Reply

  4. The GoF should have used more animals in their examples 😉

    Comment by Igor — February 26, 2008 @ 9:31 pm |Reply

  5. IMHO, The key difference between Strategy pattern and State pattern is nterchangeability. In Strategy pattern family of algorithms is interchageble which means that every algorith does the same thing but in diffrent way. States are not interchangeble. They represent diffrent situations and differnt actions for them. In my view, both of your examples are State patterns. Example for Strategy pattern could be sorting. Lets say you have to sort an array of data. You can use bubble sort, quicksort or any other sorting algorithm. Result will be same for all of them, but the means will be diffrent. You may choose algorithm depending on data properties (or you ar just lazy programmer who just want to use brute force solution, but in case it would turn to be a bottlenect you want to aplly Strategy pattern to save youself latter).

    Comment by RC — July 21, 2008 @ 8:54 am |Reply

  6. I here the newcomer. Not absolutely I will understand with topic. Explain, please.

    Comment by goolloog — November 8, 2008 @ 7:00 am |Reply

  7. A major difference is according to me, that invocation of a method of the State object usually effects replacement of the current State object while invocation of a method of the Strategy object won’t eventuate a replacement of the Strategy itself.

    In other words, State object is responsible for its replacement while Strategy pattern doesn’t.

    Comment by pcjuzer_en — December 5, 2008 @ 10:24 am |Reply

  8. …furthermore, agreeing with RC, two tiger examples both seems to me State patterns (since the Tiger changes it’s own behaviour) with the difference that in the second case invalid events will be simply ignored while in the first case they may issue an exceptional state.

    In real programming models, you also have the possibility to ignore invalid events or throw an exception. Let me name these approaches Forgiving and Strict State Pattern. 🙂

    A further difference between Strategy and State pattern can be that Strategy descendants should prepared for most possible events while State can only process a few events according to the state machine model.

    Comment by pcjuzer_en — December 5, 2008 @ 10:54 am |Reply

  9. I think that another difference is that usually concrete states know other contrete states, and they manage change status.
    Strategy “do same thinks in different way”, but the concrete strategys usually don’t know the others.

    Comment by jurgo — March 5, 2009 @ 9:58 am |Reply

  10. That article and the constructive (and wise) comments that followed, helped me understand the difference between the 2 patterns. Thanks everyone!

    Comment by Shreyas — September 6, 2009 @ 2:42 pm |Reply

  11. Good and healty discussion about the difference between the two. Tonigh I can sleep tight. Thanks everyone.

    Comment by searchlight — October 10, 2009 @ 9:54 am |Reply

  12. State:

    1. Represents an objects current state
    2. More static modelling. we need to have the finite state machine in place to model. Hence change is bit hard to incorporate
    3. State object is contained within the subject as method calls on subject are routed to state object’s methods
    4. Functional Outcome or behavior of an object is totally different in each states. Let us take ATM machine example, if it is waiting for customer it would blink the light near the card reader, user inserts the card it stops and starts interacting with the user, once transaction is done it goes back to blinking mode.


    1. Does not represent the state of the subject
    2. More dynamic modelling. If designed with DI like spring, you could induce new strategy without even touching exising code
    3. Containment is not mandatory, we could have family of strategy classes which has methods that take subject as parameter and perform the algorithm
    4. Functional Outcome is same like encrypt string or sort elements but how to that is totally different for encrypt (RSA, 3DES) for sorting (heap, bubble…)

    These are very few differences which I can think off straight away. Could be much more.

    Comment by Girish — October 22, 2009 @ 1:06 pm |Reply

  13. download link (source) is broken!

    Comment by xandu mandu — February 4, 2010 @ 11:33 am |Reply

  14. Made me understand it. Good job. =)

    Comment by TheThing — February 7, 2010 @ 1:40 pm |Reply

  15. Really grasp the point, Appreciate that!

    Comment by Felix.Huang — June 5, 2010 @ 11:46 pm |Reply

  16. Thanks for the post and comments, all are useful.

    Comment by Chukked — August 11, 2010 @ 7:36 am |Reply

  17. Sorry for my bad English but i think it can be usefull.
    Lets Suppose we have developing game and now we need to decide what way our application must to go menu or map view or return to game. If player going in map view and press esc button game must switch in game state.And if it’s in game state and user press esc button it must be go in menu view. It’s will be more better develop such program using State pattern becouse in that way we can avoid using many conditional state(example if we use strategy pattern). Now lets suppose we need to get data from server for our application. And becouse our application stabil working is very important we get data from 2 different server. But problem is in that server return data type is different JSON and XML. Good solution will be universal converter
    what wiill convert result(json,xml…) to ResultDTO. In this case we can see advantage of strategy pattern

    Comment by Vladimir — August 22, 2011 @ 4:06 am |Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: