How Strategy Design Pattern is Different From State Design Pattern in Java ?

State design pattern is a behavioral design pattern that is use for defining the state machine for an object. Each state of an object is defined in a child class of State class. When different actions are taken on an Object, it can change its state.

Strategy pattern is also a behavioral pattern, but it is mainly used for defining multiple algorithms. With same action of a client, the algorithm to be used can change.

Some people consider State pattern similar to Strategy pattern, since an Object changes its Strategy with different method invocations. But the main difference is that in State pattern internal state of an Object is one of the determining factors for selecting the Strategy for change of state.

Where as in Strategy pattern, client can pass some external parameter in input during method invocation that determines the strategy to be used at run time.

Therefore State pattern is based on the Object’s internal state, where as Strategy pattern is based on Client’s invocation.

State pattern is very useful in increasing the maintainability of the code in a large code-base.



You May Interest

Why Java Does Not Support Operator Overloading ?

What is the Difference Between sendRedirect and forward in a JSP ...

What are the Similarities Between HashSet and HashMap in Java ?

How Can We Print an Array in Java ?

Why Should You Define a Default Constructor in Java ?