No such thing as a small change | |
PerlMonks |
Re: Re: Creating Common Constructorby jmanning2k (Pilgrim) |
on Jul 10, 2003 at 17:16 UTC ( [id://273057]=note: print w/replies, xml ) | Need Help?? |
Still some things here that don't make sense from a OO perspective.
Like you said previously, a Zoo has Animals, an Animal is not a Zoo. Therefore, Animals shouldn't really be a subclass of Zoo. Your implementation still hard codes the animal types into the Zoo class. If you want to add a new type of animal, there are a lot of changes to make. Instead, the count methods should be members of the Animal subclasses. Zoo should have an array of objects of type Animal, and be able to get the count for each one. Here's my ideal design:
Zoo.pm - A zoo. This has an array that holds many Animal objects. It knows nothing about specific Animal subclasses. It can also have a factory method that creates a new animal and adds it to the zoo collection. Animal.pm - These are Animal objects. This is a interface type class that merely defines specific methods that Zoo will use. The factory could also logically be here Animal::Llama - Subclass of Animal that holds Llamas. Animal::Camel - Subclass of Animal that holds Camels. This way, a Zoo has a collection (array) of Animals. To get the counts of each animal type, try this:
Get it? The Zoo should hold Animals, and know nothing about subtypes of animals. Exception: - The factory method can know about animal types. That way it can create the appropriate subclass.
This is rough and incomplete. (I would add a Animal::Collection object - a zoo has many collections of animals. A collection is a number of individual animals.) But, this is a better design. To add a new animal type, you just make two changes - the new class, and add it to the factory method. Is this clear? I'm happy to clarify if you still need help.
In Section
Seekers of Perl Wisdom
|
|