lundi 6 avril 2015

How to refactor code to avoid multiple if-s?

On interview I was asked the following question:


I have following method:



public void foo(SomeObject o){
if(o.matches(constant1)){
doSomething1();
}else if(o.matches(constant2)){
doSomething2();
}
}else if(o.matches(constant3)){
doSomething3();
}
....
}


question was: you should refactor method above. What will you do?


On interview I didn't grasp how to make it.


Now I think that state design pattern is suitable for this task ?


Am I right? What do you think?


P.S.


I negotiated with my colleague. he thinks that strategy design pattern is more suitable.


Aucun commentaire:

Enregistrer un commentaire