• 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

Pattern to replace switch case with different funtionality in the case block

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a situation here, below is the extract


I want to get rid of the switch statement, reading through one of Martin Fowler's book found that this can be replaced by adding appropriate subclass (for each case condition) and using the same polymorphically in this method.

However, as can be seen by the method getting invoked, they dont seem to be doing similar work, so subclassing them with a common parent would not yeild a common behavior(method) to be abstracted out in the parent class.

Is there any way to get around this situation, or am I better off creating different methods for each case statement?

Cheers-
Joshua
 
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
Command pattern.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subclassing using overloaded ("virtual") functions is an effective, efficient and powerful technique, but your sample code doesn't look like a good fit, since it looks more like you're interested in extracting different selections of data from a SINGLE object than in getting different data from different instances of an object. The fact that the destination data is always of the same type is immaterial.

In practical terms, I would probably keep the switch statement. It's simple, easy to read and unambiguous. And as a plus, most compilers contain extensive functions to optimize switch statements to a bare minimum of overhead.

I might change my mind if things got more complicated, but for the example given, I'd probably not lose sleep over it.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would move the conditional logic into the storeManager class and have a common fetch method. For example,



In my opinion, it would best to keep this type of logic outside of a Servlet class, and would be more appropriate inside the storeManager.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic