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.
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!