Sometime we need to carry out several computation / algorithms depending on certain conditions. We go ahead in implementing those generally by applying either switch or ternary operator or if else. Though initially somehow we manage to write those program but if the program demands too complex then it is difficult to frame such as well as to maintain.
Moreover, writing all the logic at a single place is not at all advisable as it yields into tight coupling.
Background
In many situations we come across to write computational logic/ algorithms which we generally accomplish by using if else / switch or ternary operator. It becomes difficult at time to write such a program and later on adds a lot of cost while maintenance.
Rescuer
Strategy design pattern. It comes under the category of Behavioral Patterns
How
a)It decouples the client and the algorithm/ computation logic in separate classes
b)Helps to switch algorithms at any time
c)Easily allow to plug in a new algorithm.
Pattern Components
The strategy pattern comprises of the following components
a)Strategy Interface –
Interface common to all concrete strategies.
b)Concrete Strategies / Different algorithm classes-
Various concrete classes that implement the strategy interface for the sake of algorithm implementation specific to itself.
Read more: Codeproject