mardi 12 juillet 2016

Replacing if else statements

I have a code which looks something like this:

if(num == 1) {
return new Alg1();
} else if (num == 2) {
return new Alg2();
}
...
else if (num == n) {
return new AlgN();
}

I have tried to use the Strategy pattern but it seems like it does not satisfy the task to reduce the if statements, can you please suggest me some other way to do it, thanks

Aucun commentaire:

Enregistrer un commentaire