• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which design should I use?

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use Factory and Command patterns.
Use Factory to create a command object and call command.execute().
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
implementing Factory (Abstract Factory - if needed depending on the complexity) with Command pattern is good solution.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chain of Responsibility will be good for this scenario.
reply
    Bookmark Topic Watch Topic
  • New Topic