Strategy Pattern
Strategy Pattern
A Strategy Pattern says that "defines a family of functionality, encapsulate each one, and make them interchangeable". That is, in Strategy pattern, a class behaviour or its algorithm can be changed at run time.
The Strategy Pattern is also known as Policy.
Benefits:
- It provides a substitute to sub-classing.
- It defines each behaviour within its own class, eliminating the need for conditional statements.
- It makes it easier to extend and incorporate new behaviour without changing the application.
Usage:
- When the multiple classes differ only in their behaviours.e.g. Servlet API.
- It is used when you need different variations of an algorithm.
Strategy Pattern in (Core Java API's) or JSE 7 API's:

Strategy Pattern in (Advance Java API's) or JEE 7 API's:


Comparing our design to the definition of strategy pattern encapsulated kick and jump behaviors are two families of algorithms. And these algorithms are interchangeable as evident in implementation.
Below is the Java implementation of the same.
Output :
Ken Default Punch Tornado Kick Short Jump Long Jump
Reference: https://www.geeksforgeeks.org/strategy-pattern-set-2/
Comments
Post a Comment