• 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

Refactoring: Replace Conditional with Polymorphism

 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i read through fowler's refactoring: Replace Conditional with Polymorphism.

fowler presents this kind of solution:




but why isn't fowler going even further and uses 'instanceof':



to me the latter code is a better solution. does anybody know why fowler is not going for the more typesafe 'instanceof'? he must have had a reason...

thank you.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you quoted is not the solution, it's the original, unrefactored code. The solution uses polymorphism instead of the switch statement.
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops,

i got a bit confused (because i read some lines not properly). it makes sense now.
of course payAmount() gets declared abstract in the superclass, subclasses implement it and switch statement gets kicked out.

thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic