Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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:
my %count; foreach my $animal in (@zooanimals) { # $animal is a Animal object. my $animal_name = $animal->get_name(); $count{$animal_name}++; } # another loop here to print %count.

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.
sub add_animal() { my $type = shift; if($type == "Llama") { my $newanimal = new Animal::Llama; } elsif {$type == "Camel") { my $newanimal = new Animal::Camel; } push (@allanimals, $newanimal); } my $zoo = new Zoo; $zoo->add_animal("Llama"); $zoo->add_animal("Llama"); $zoo->add_animal("Camel"); $zoo->count_animals(); __OUTPUT__ There are 2 Llamas in the zoo. There are 1 Camels in the zoo.

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 reply to Re: Re: Creating Common Constructor by jmanning2k
in thread Creating Common Constructor by DeadPoet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-26 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found