Open In App

Difference Between State and Strategy Design Pattern in Java

Improve
Improve
Like Article
Like
Save
Share
Report

It is important for a java developer to clearly understand the difference between State and Strategy Design Pattern in Core Java application in order to make the proper use of them. 

  • Though both State and Strategy design patterns have a similar structure, and both of them are based upon the Open closed design principle, represents ‘O’ from SOLID design principles, they are totally different in their intent.
  • The strategy design pattern in Java is used to encapsulate a related set of algorithms to provide runtime flexibility to the client. The client can choose any algorithm at runtime, without changing the Context class, which uses a Strategy object.
  • Some popular examples of Strategy patterns is writing code, which uses algorithms e.g. encryption, compression, or sorting algorithm.

One can now clearly see the difference between Strategy and State pattern, their intent is different. State pattern helps object to manage state, while Strategy pattern allows the client to choose different behavior. Another difference, which is not easily visible is, who drives change in behavior.

In the case of Strategy pattern, it’s client, which provides a different strategy to Context, on State pattern, the state transition is managed by Context or State itself. Also, if you are managing state transition in State object itself, it must hold reference of context so that it can call setState() method to change current state of Context. A vending machine is one of the most popular examples.

So by far, we noted that the State and Strategy are similar in structure and their intent is different. Hence, below are the primary key differences between State and Strategy Design Pattern in Java they both look similar in their class both of them enforce Open Closed design principle and encapsulate behaviors. Use Strategy design pattern, to encapsulate algorithm or strategy, which is provided to Context at runtime, maybe as a parameter or composed object, and use State Pattern for managing state transitions in Java.

                              STATE PATTERN                                    STRATEGY PATTERN
In-State pattern, an individual state can contain the reference of Context, to implement state transitions. But Strategies doesn’t contain the reference of Context, where they are used.
State encapsulate state of an Object. While Strategy Pattern encapsulates an algorithm or strategy.
State pattern helps a class to exhibit different behaviors in a different state. Strategy Pattern encapsulates a set of related algorithms and allows the client to use interchangeable behaviors through composition and delegation at runtime.
The state is part of the context object itself, and over time, the context object transitions from one State to another. It can be passed as a parameter to there the Object which uses them e.g. Collections.sort() accepts a Comparator, which is a strategy.
State Pattern defines the “what” and “when” part of an Object. Example: What can an object when it’s in a certain state. Strategy pattern defines the “How” part of an Object. Example: How a Sorting object sorts data.
Order of State transition is well-defined in State pattern. There is no such requirement for a Strategy pattern. The client is free to choose any Strategy implementation of his choice.
Change in State can be done by Context or State object itself. Change in Strategy is done by the Client.
Some common examples of Strategy Patterns are encapsulating algorithms e.g. sorting algorithms, encryption algorithms, or compression algorithms. On the other hand, recognizing the use of State design patterns is pretty easy.
If you see, your code needs to use different kinds of related algorithms, then think of using a Strategy pattern. If you need to manage state and state transition, without lots of nested conditional statements, state pattern is the pattern to use. 

Last Updated : 22 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads