I am not clear on what advantage does strategy pattern offers over simple if-else.
Example of poor code
//poor code that we all can agree is not the way to go
----- main class -----
String strategyname;
//lets assume at this point variable strategyname has a value assigned somehow at runtime
if(strategyname == 'algorithm1'){
//long code for algorithm1
}elsef(strategyname == 'algorithm2'){
//long code for algorithm2
}
Disadvantage of above code is that if we need to add another algorithms, we touch the main class (which could have other functionalities built into it as well), so that increases the probability of us breaking some other functionality. Plus class starts to become increasingly long. And unit testing becomes tough.
Now ideally, as per java design pattern book goes, we shold use strategy desig pattern. It would go something like this
//strategy design pattern
------ main class -----
algorithmsInterface selectedAlgorithm = algorithmClassFactory.getAlgorithm(strategyname);
selectedAlgorithm.doTheJob(commonParameter);
------ factory class that returns instance of appropriate class ---
class algorithmClassFactory{
public static algorithmsInterface getAlgorithm(String strategyname){
if(strategyname == 'algorithm1') {
return new algorithm1Class();
}elseif(strategyname == 'algorithm2'){
return new algorithm2Class();
}
}
}
------ interface that all algorithms classes need to implement
interface algorithmsInterface{
public void doTheJob(commonParameter);
}
------ class for algorithm1 -----
class algorithm1Class implements algorithmsInterface{
public void doTheJob(commonParameter){
//long code for algorithm1
}
}
------ class for algorithm2 -----
class algorithm2Class implements algorithmsInterface{
public void doTheJob(commonParameter){
//long code for algorithm2
}
}
I understand we are able to use polymorphism here. Main class doesn't need to be touched when we add another algorithm, as we can just add a new class for it and modify the algorithmClassFactory class to include that class as well. But what advantage does it offer over below code, where we dont use interface, and just directly call the appropriate class from factory class-
//why cant we do as below
------ main class -----
algorithmClassFactory.getAlgorithm(strategyname);
------ factory class that calls the appropriate algorithm class ---
class algorithmClassFactory{
public static algorithmsInterface getAlgorithm(String strategyname){
if(strategyname == 'algorithm1') {
algorithm1Class algo1 = new algorithm1Class();
algo1.doTheJob(commonParamter);
}elseif(strategyname == 'algorithm2'){
algorithm1Class algo2 = new algorithm2Class();
algo2.doTheJob(commonParamter);
}
}
}
------ class for algorithm1 -----
class algorithm1Class{
public void doTheJob(commonParameter){
//long code for algorithm1
}
}
------ class for algorithm2 -----
class algorithm2Class{
public void doTheJob(commonParameter){
//long code for algorithm2
}
}
Without the use of interface (polymorphism) i am forced to repeat the line of calling doTheJob method of each class. I guess we can call it a disadvantage. But is this the only advantage Strategy design pattern offers over this last design? With above code, am i violating any OOPS design principle like Separation of Concern, single responsibility, etc? Am i wrong in assuming that even last design follows Open-closed principle?