This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes design pattern to avoid lot of if-else condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "design pattern to avoid lot of if-else condition" Watch "design pattern to avoid lot of if-else condition" New topic
Author

design pattern to avoid lot of if-else condition

Abhishek Mhptr
Ranch Hand

Joined: Aug 09, 2009
Posts: 33
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

Joined: Jun 22, 2009
Posts: 150
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

Joined: Jul 05, 2005
Posts: 1936
You can use Map.


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
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
Kengkaj Sathianpantarit wrote:You can use Map.


Please let me know what map is

Thank yhou in advance
Abhishek Mhptr
Ranch Hand

Joined: Aug 09, 2009
Posts: 33
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

Joined: Jul 05, 2005
Posts: 1936
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

Joined: Jul 05, 2005
Posts: 1936
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

Joined: Jul 05, 2009
Posts: 257
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


Experience and talent are independent of age
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Or we could use Factory Method and Command patterns.

Use the Factory to get a Command object and invoke Command#execute().
 
 
subject: design pattern to avoid lot of if-else condition
 
Similar Threads
what are the advantages of using Null object pattern?
sum() as not seperate column
single or multiple servlets
Entities
RegularExpression in Java