• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which design should I use?

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my class I have a condition statement which is very large

If (condtion1 ,condition 2,condtion4)
Set of statement

Else condtion 13
Set of statement

Condtion 5,condition 6,
Set of statement

Condtion 7 condtion 8 condtion 9

Set of statement

else condition 10 ,condition 11,condition 12

Set of statement


.

Since I every thing hard coded in If loop, I do not want to use it,

How I can replace the If loop with more generic stuff, which allow me to add more conditions and remove more conditions?
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A switch statement would be helpful first approach if your conditions are numeric.

Are your conditions something like variableX=1 (a number)?

Will you be operating on the same data on your statements? That is, you could put that in a method who's parameters would be the same even if what each method does is different?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Factory and Command patterns.
Use Factory to create a command object and call command.execute().
 
kitarp Prabhu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
implementing Factory (Abstract Factory - if needed depending on the complexity) with Command pattern is good solution.
 
Laurent Leonard
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chain of Responsibility will be good for this scenario.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic