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?