Saturday, August 27, 2022

Difference between State and Strategy Design Pattern in Java

Hello guys, if you are wondering what is difference between State and Strategy pattern in Java then you are at the right place. In order to make proper use of State and Strategy design Patterns in any Java application, it's important for a Java developer to clearly understand the difference between them. Though both State and Strategy design patterns have a similar structure, and both of them are based upon the Open closed design principle, which represents 'O' from SOLID design principles, they are totally different on 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. One of the popular examples of Strategy patterns is writing code, which uses algorithms like encryption, compression, or sorting algorithm.

On the other hand, the State design pattern allows an object to behave differently at different state. Since real-world object often has state, and they behave differently at different state, e.g. a Vending Machine only vend items if it's in hasCoin state, it will not vend until you put the coin on it.

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

In case of Strategy pattern, it's client, which provides different strategy to Context, on State pattern, 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 e.g. Vending Machine, so that it can call setState() method to change current state of Context.

On the other hand, the Strategy object never held reference of Context, it's the client which passes the Strategy of there choice to Context. As difference between state and strategy pattern is one of the popular Java design pattern question on Interviews.

In this Java design pattern article, we will take a closer look on this. We will explore some similarities and differences between Strategy and State design patterns in Java, which will help to improve your understanding on both of these patterns.

Btw, If you are serious about learning design patterns and principles, I suggest you take a look at these Java design pattern online courses for beginners and experienced  This course covers both SOLID design principles like Open Closed and Liskov substitution, and all-important Object Oriented design patterns like Decorator, Observer, Chain of Responsibility, and much more.





Similarities between State and Strategy Design Pattern in Java

If you look at the UML diagram of State and Strategy design Pattern, they both look very similar to each other. An object that uses a State object to change its behavior is known as Context object, similarly an Object which uses a Strategy object to alter its behavior is referred as Context object. 

Remember client interact with Context object. In case of state pattern, context delegates method calls to state object, which is held in form of current object, while in case of strategy pattern, context uses Strategy object passed as parameter or provided at the time of creating Context object.

UML Diagram of State Pattern in Java
State design Pattern in Java



This UML diagram is for state design pattern, drawn for a classic problem of creating object oriented design of Vending Machine in Java. You can see that State of Vending Machine is represented using an interface, which further has implementation to represent concrete state. Each state also holds reference of Context object to make transition to another state due to action triggered by Context.

UML Diagram of Strategy Pattern in Java
Strategy Design Pattern in Java UML diagram



This UML diagram is for strategy design pattern, implementing sorting functionality. Since there are many sorting algorithms, this design pattern lets the client choose the algorithm while sorting objects. In fact, Java Collection framework makes use of this pattern to implement the Collections.sort() method, which is used to sort objects in Java.  

The only difference is instead of allowing clients to choose sorting algorithms, they allow them to specify comparison strategy by passing instances of Comparator or Comparable interface in Java.

Let's see couple of more similarities between these two core Java design patterns  :

1. Both State and Strategy Pattern makes it easy to add new state and strategy, without affecting Context object, which uses them.

2. Both of them, makes your code follow open closed design principle, i.e. your design will be open for extension but closed for modification. In case of State and Strategy pattern, Context object is closed for modification, introduction of new State or new Strategy, either you don't need to to modify Context of other state, or minimal changes are required.

3. Just like Context object is started with a initial state in State design Pattern, a Context object also has a default strategy in case of Strategy pattern in Java.

4. State pattern wraps different behavior in form of different State object, while Strategy pattern wraps different behavior in form of different Strategy object.

5. Both Strategy and State Patterns relies on sub classes to implement behavior. Every concrete strategy extends from an Abstract Strategy, each State is sub class of interface or abstract class used to represent State.




Difference between Strategy and State Pattern in Java?

So now we know that State and Strategy are similar in structure and there intent are different. Let's revisit some of the key difference between these design patterns.

1) Strategy Pattern encapsulates a set of related algorithms, and allow client to use interchangeable behaviors through composition and delegation at runtime, On the other hand State pattern helps a class to exhibit different behaviors in different state.

2) Another difference between State and Strategy Patten is that, State encapsulate state of an Object, while Strategy Pattern encapsulate an algorithm or strategy. Since states are cohesively associated with object, it can not be reused, but by separating strategy or algorithm from it's context, we can make them reusable.

3) In State pattern, individual state can contain reference of Context, to implement state transitions, but Strategies doesn't contain reference of Context, where they are used.

4. Strategy implementations can be passed as parameter to there the Object which uses them e.g. Collections.sort() accepts a Comparator, which is a strategy.  On the other hand state is part of context object itself, and over time, context object transitions from one State to other.

5. Though both Strategy and State follows Open closed design principle, Strategy also follow Single Responsibility principle, Since every Strategy encapsulate individual algorithm, different strategies are independent to each other. A change in one strategy, doesn't order a change in another strategy.

6. One more theoretical difference between Strategy and State pattern is that former defines "How" part of an Object like how a Sorting object sorts data, One the other hand State Pattern defines "what" and "when" part of Object e.g. What can an object, when it's on certain state.

7. Order of State transition is well defined in State pattern, there is no such requirement for Strategy pattern. Client is free to choose any Strategy implementation of his choice.

8. Some of the common examples of Strategy Pattern is to encapsulate algorithms e.g. sorting algorithms, encryption algorithms or compression algorithms. If you see, your code needs to use different kind of related algorithms, than think of using a Strategy pattern. On the other hand, recognizing use of State design pattern is pretty easy, if you need to manage state and state transition, without lots of nested conditional statement, state pattern is the pattern to use.

9. Last but one of the most important difference between State and Strategy pattern is that change in Strategy is done by Client, but Change in State can be done by Context or State object itself.



That's all on the difference between State and Strategy Pattern in Java. As I said, they both look similar in their class and UML diagrams, 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.

Other Java Design Patterns tutorials you may like
  • 5 Free Courses to learn Object Oriented Programming (courses)
  • How to design a Stock Position Calculator in Java? (tutorial)
  • When to use Command Design Pattern in Java (example)
  • How to create thread-safe Singleton in Java (example)
  • 7 Best Courses to learn Design Pattern in Java (courses)
  • Difference between Factory and Abstract Factory Pattern? (example)
  • 10 OOP Design Principle Every Java developer should learn (solid principle)
  • How to implement the Strategy Design Pattern in Java? (example)
  • 7 Best Books to learn the Design Pattern in Java? (books)
  • 20 System Design Interview Questions (list)
  • 18 Java Design Pattern Interview Questions with Answers (list)
  • Difference between Factory and Dependency Injection Pattern? (answer)
  • How to Prepare for System Design Interview (System Design Prep)
  • 5 Free Courses to learn Data Structure and Algorithms (courses)
  • 7 Books to learn System Design for Beginners (System design books)

Thanks a lot for reading this article so far. If you like these difference about State and Strategy Design Patterns  and find this article worth your time then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.


5 comments:

  1. Strategy and Command difference description will be even more interesting! Write more!

    ReplyDelete
  2. Could you post the code too?

    ReplyDelete
  3. In my opinion real difference between state and strategy pattern is stateful vs stateless. State pattern is always stateful but Strategy will not. In fact, Strategy pattern is nothing but a clever way of achieving functional programming in Java, even before Java 8. You can pass behaviours to methods encapsulated in strategy object, a smart way of method injection. State is rather more useful and complex pattern, you can implement state machine, core of many program using this pattern.

    ReplyDelete
  4. Patterns are a map, a guideline.
    Code is the terrain, sometimes it's rocky.
    It's nice to have a map.
    Don't always trust the map, trust the terrain instead.
    -Miguel Ping
    http://blog.codinghorror.com/rethinking-design-patterns/

    Design Patterns are identified and applied via
    1). Code Refactoring
    2). Code Optimization

    without these 2 required skills trying to learn design patterns is useless in my opinion.
    (Knew java programmers who tried and it was awful).

    For different systems have diff. requirements (which is all about balance)

    that is why there are specific design patterns (GoF or Non-GoF) that can have many forms.

    eq. Singleton - Double-Checked Locking , Lazy, and so on...

    also programming paradigm in java is also changing therefore most resources will be out-of-date

    Annotation (Compile-Time weaving)
    Lamda Expressions
    etc..

    my advice will be to be a pragmatic programmer opposite of dogmatic programmer
    (who's trying to apply Design Pattern base from reference/or by the book).

    ReplyDelete
  5. very nice and detailed explanation.Appreciate it.

    ReplyDelete