![]() |
Sponsored Link •
|
Advertisement
|
Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
Message:
The first step that you need to take is to reconsider your class structure. In a switch you normally case over some piece of data. Can you turn that data into an object. Switch statements are a good indication that the object structure may be lacking (however, you cannot always avoid case). Consider this switch: void printDayOfTheWeek( int day_of_the_week ) { Do this instead: void printDayOfTheWeek( Day day ) { Data validation is one place where it is difficult (if not impossible) to remove a switch. However, in other places, you need to turn your data into objects. In OO you don't want to ask your objects to give you data and then process that data. Instead, you want to ask an object to do something to its data. switches generally break from this way of thinking. Check out Refactoring by Martin Fowler for an entire section about killing switches. Regards > I have lot of 'switch' statements which really > Is there a design pattern which eliminates the need for te switch statements ? > thanks
Replies: |
Sponsored Links
|