Strategy Pattern Design is of great significance when implementing a program logic that can be invoked depending on certain conditions. It’s generally used when the program demands are way too complex. Before you begin using strategy pattern, it’s important to understand what it essentially is.
Strategy Pattern Design
The Strategy Pattern Design is one of the most interesting parts of the advanced java training. It’s interesting because this is where you need to rise above the ‘switch’ and ‘if else’ and get to understand pattern behavior.
While the ‘switch’ and ‘if else’ operators can certainly get you though with the standard programs, when the program demands get complex and require execution of several algorithms and computations, it doesn’t work. Moreover, stuffing all your logic in a single place is a bad idea. Hence the use of the strategy pattern.
What role does it play?
The strategy pattern:
- Decouples the computation/ algorithm and the client in separate classes
- Helps switch to different algorithms easily at any time
- Allows easy plugging in of a new algorithm
What are its key components?
The key components of strategy pattern design are:
- Strategy interface, which is common to every concrete strategy
- Different algorithm/ concrete strategy classes, which assists in implementing the strategy interface to enable algorithm implementation that’s specific to itself.
- Context classes, which delegate requests to the respective concrete strategies originating from the client’s end.
- Client, which refers to the Window form.