Re: Design question
On 12 Feb, 22:34, John <printdude1...@gmail.com> wrote:
Then I started thinking about things like, "what's the difference
between a domestic dog and a wolf". Well for one thing, wolves hunt in
packs. Domestic dogs do not. So, in my very limited thinking, the Dog
class may have an instance variable (what dogs know about) called
huntInPacks which is a boolean. And each subclass of Dog, when it
constructs a Dog, will pass in a true or false depending on what type of
dog it is.
Thank you, John, for explaining further what you're doing; this is
exactly the sort of thing I did myself when I was learning the
wonderful world of OO; I'm sure it's the most common way of
investigating inheritance. Enjoy the ride.
Given the context clarification, however, your above two sentences are
ever-so slightly worrying, but again temper everything I say with a
pinch of salt and a barrelful of Ed-still-doesn't-get-it.
Firstly, your huntInPacks instance variable in Dog. You seem to
suggest that subclasses of Dog will use this variable to decide
whether they can hunt in packs. You may have good reasons for this,
but OO usually rules against such a variable.
Instead of having such an instance variable in Dog to decide the
behaviour of subclasses, it is usually the subclass itself that
decides. In other words the behaviour is defined by the subclass,
whereas the interface is defined by the superclass.
For example, given the context, your Dog class will probably have a
method hunt(). What I think (I may be wrong) you have described is
that your Wolf implementation of this method will be (forgive my chain-
brackets appearing as square brackets - don't ask):
void hunt() {
if (huntInPacks) {
doHuntInPacks();
} else {
doHuntSolo();
}
}
This is wrong, OO-wise.
Instead, the subclass itself will define how the hunting is performed.
So for a Wolf, the hunt() method could be implemented as:
void hunt() {
Collection otherWolves = getNearestWolves();
signalHuntBegin(otherWolves);
}
Note that the Wolf sublcass knows exactly how it hunts: it hunts in
packs, and it won't hunt any other way.
Whereas the Fox will implement the method as:
void hunt() {
Target chicken = getTarget();
attack(chicken);
}
The Fox sublcass knows it doesn't hunt in packs without checking any
information in the parent class, because this is what Foxes do, and
this instance knows it's a Fox.
The other worrying aspect of your above two sentences is that you
write, "And each subclass of Dog, when it constructs a Dog ..."
What do you mean by this? It is rare that a subclass creates a super-
class (though it can happen), but it seems even more rare that a Fox
would create an instance of Dog.
I just want to make sure that you don't think that any subclass must
create a super-class. Rememeber: when Fox extends Dog, and something
creates an instance of Fox, there is only one instance created: there
are not two separate instances of Fox and Dog created.
..ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition