How to refactor code to avoid multiple if-s [from interview]? <p>On interview I was asked the following question:</p> <p>I have following method:</p> <pre><code>public void foo(SomeObject o){ if(o.matches(constant1)){ doSomething1(); }else if(o.matches(constant2)){ doSomething2(); }else if(o.matches(constant3)){ doSomething3(); } .... } </code></pre> <p>question was: <code>you should refactor method above. What will you do?</code></p> <p>On interview I didn't grasp how to make it.</p> <p>Now I think that <code>state</code> design pattern is suitable for this task ?</p> <p>Am I right? What do you think?</p> <h2>P.S.</h2> <p>I negotiated with my colleague. he thinks that <code>strategy</code> design pattern is more suitable.</p> <h2>P.S.</h2> <p>Another expert thinks that <code>chain of responsibility</code> design pattern is more suitable.</p>