Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

design pattern to avoid lot of if-else condition

 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all..
i am new to java design pattern .
i wish to create a design pattern to avoid lot of if-else conditions in a program.
for example, if the input is : "1", output is : "one" .
input:2 -> output:"two"
it goes till 50. it means there are 50 condition statements.
i need to avoid these many if-else condition.

please suggest me a design pattern for this and please help me out in creating the implementation part of it.

thanking you.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Mohapatro wrote:hi all..
i am new to java design pattern .
i wish to create a design pattern to avoid lot of if-else conditions in a program.
for example, if the input is : "1", output is : "one" .
input:2 -> output:"two"
it goes till 50. it means there are 50 condition statements.
i need to avoid these many if-else condition.

please suggest me a design pattern for this and please help me out in creating the implementation part of it.

thanking you.



Use array to store all the strings(one,two...). Use the input as the index of the array to diplay as you require. Say if i=input then the index a[i] will give you the right answer.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Map.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:You can use Map.


Please let me know what map is

Thank yhou in advance
 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for all your replies.
the condition (1->one, 2->two) , which i had mentioned was just an example.
that was not the exact requirement.
it can be any input and any output.

can anyone please suggest how to implement in map??

thanking you
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi Lohi wrote:
Kengkaj Sathianpantarit wrote:You can use Map.


Please let me know what map is

Thank yhou in advance

http://java.sun.com/javase/6/docs/api/java/util/Map.html
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Mohapatro wrote:thank you for all your replies.
the condition (1->one, 2->two) , which i had mentioned was just an example.
that was not the exact requirement.
it can be any input and any output.

can anyone please suggest how to implement in map??

thanking you

Read http://java.sun.com/javase/6/docs/api/java/util/Map.html.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If here we assume that each option pertains to a full fledged functionality, mutually independent of any other(mutually exclusive), being carried out in itself, then you need as many methods as options because you would put each functionality in a separate method. These methods should all be behaviors of a separate class.
To invoke a particular method of a class you can create the method name at runtime using a Map. Then use reflection to call the required method or otherwise.

Regards
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or we could use Factory Method and Command patterns.

Use the Factory to get a Command object and invoke Command#execute().
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic