• 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 When The Class Changes

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is going to be a bit vague, but I'll do my best to make it clear.

I have a class with several methods that take references to another class as their parameters, return lists of such references as their return values, or both. Many of those methods apply the methods of the class of the parameters to those parameters. This isn't my code (which is way too long and complicated to post), but, in essence, I have this:


Now, my Vegetable class has a few other methods besides diceYourself, but no method in the Blender class uses any of those. For example Vegetable might look like this:



Blender uses diceYourself, but not eatLess.

Now, suppose I create a new class, Fruit, that also has a diceYourself method, lacks an eatLess method, and has a new method unique to Fruit:


I want Blender to be able to handle both classes. Seems to me that this calls for an interface:


Which both Vegetable and Fruit should implement:



And now, the Blender class must simply be rewritten to take references to Produce objects, not Vegetable objects:



I expect that would work. Here are my (vague) questions:

1. Is there a superior alternative, or does this make sense?
2. Should I have spec'ed out the Produce interface and written Blender in term of that interface in the first place?
3. If the answer to 2 is "yes," how does one know, in general, when to create classes and interfaces, as opposed to merely classes?
4. Should one always create interfaces with classes?
5. (optional) How stupid do I look for asking any of this?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. Is there a superior alternative, or does this make sense?


This makes sense. I would recommend using a more generic interface name like Blendable or Diceable. One can stick other things in a blender, right? This will prevent the code from needing to change when you decide you want to make meatball puree .

2. Should I have spec'ed out the Produce interface and written Blender in term of that interface in the first place?


No. You can't predict what will be needed and creating interfaces that turn out to not be needed makes the code hard to read. Notice how the Blender class is backward compatible so you aren't breaking things for the callers.

4. Should one always create interfaces with classes?


No. Wait until you need them.

5. (optional) How stupid do I look for asking any of this?


Not at all . it was a very clear and well thought out question.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jeanne. Very reassuring.
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic