How is Strategy design pattern different from State design pattern in Java?

0
417

Strategy design pattern in Java

Strategy design pattern is different from state design pattern in Java. Both the Strategy design and State design has a structure that is similar between the two designs and depending on open structure. The strategy design encapsulates a various set of algorithms for runtime execution. With strategy design, Java programmer can choose which algorithm to use at the runtime. The strategy object created is used so no need to change any classes for choosing the algorithm at run time. This is possible with the delegation process. For a client it’s easy and they can choose whichever strategy of their choice. Any change to be made can be done by strategy design. The focus of strategy design is how to get the results expected from the method. To get the results the code is written to how to perform the methods of the algorithms.

Use of strategy design in Java

The strategy design is used in sorting algorithms. Sorting algorithms sorts the data either in ascending or descending orders. It also is used when compressing data to use less storage space. During encryption method strategy design is used so that data is safe and secure in encrypted form. It depends on client’s choice to which strategy to simply on the behavior and how it should act.

State Design Pattern in Java

State design pattern in Java is similar yet different. The state design pattern object behavior varies at different states. Its focus on the real world like object. Due to that, it behaves differently in the different states. That particular state being managed is when state design pattern is used. At each state, the behavior will be different due to changes in every state. It also encapsulates but the object, not the algorithm like in strategy design. The changes cannot be made by the client but the changes exist due to the state of an object and some other parameters. The programmer must know which states are available to take the advantage of this design in implementation. While the strategy method focuses on how to get the expected results, the state design differs its focus and concerns with what state and type of states are available to use.

Use of state pattern in Java

The state pattern design is used for managing purpose. It manages the relationships between the various objects. As well as the various algorithms required to use. It allows the changes in behavior in the internal state of the objects, relationships, algorithms, etc. It encapsulates the behavior but focuses on the state for the request made.

LEAVE A REPLY

Please enter your comment!
Please enter your name here