As Gerardo said, the best answer for this may depend on the nature of the decisions you have to make.
Sometimes things like this can be encapsulated in a simple jump table (which in Java looks like a map of conditions => interface implementatons).
Joined: Jul 05, 2005
You could use Factory and Command patterns.
Use Factory to create a command object and call command.execute().
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Joined: Nov 12, 2008
implementing Factory (Abstract Factory - if needed depending on the complexity) with Command pattern is good solution.
Joined: May 15, 2001
You could use the chain of responsability pattern (gof).
For each block in your if statement, you have a class that check if some conditions are validated, and if it's the case you can execute a statement.
Main -> Handler1 -> Handler2 -> Handler3 -> ... -> HandlerN
The advantages are
- different handlers could be triggered.
- Adding a new Handler is obvious.
- The condition are easier to read because no complex if statement.
- could be implemented in a way that if a statement is executed, the iteration on the chain is stopped
The disadvantage are
- be carefull of overlapping conditions (more than one statement is executed)
- testing could be difficult
Anand Kumar Singh
Joined: Oct 18, 2007
Chain of Responsibility will be good for this scenario.