• Post Reply Bookmark Topic Watch Topic
  • New Topic

Refactoring When The Class Changes  RSS feed

 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jeanne. Very reassuring.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!