Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
A recurring pattern that occurs is that you want to construct instances from a class hierarchy based on some typeless data (eg, deserializing a string).

Taking a typical barnyard example:

package Animal; package Sheep; @ISA = ('Animal'); package Cow; @ISA = ('Animal'); package Pig; @ISA = ('Animal');

A new animal wanders into your program and identifies itself with a noise. Either "baa", "moo" or "oink".

You could do this:

sub make_animal { my ($sound) = @_; if ($sound =~ /baa/) { return Sheep->new; } if ($sound =~ /moo/) { return Cow->new; } if ($sound =~ /oink/) { return Pig->new; } }

The problem is that each time you define a new animal, you have to update your make_animal function.

What would be better is if within the class somehow the animal "registered" itself against the sound it makes.

We can do that by creating a global hash, and then each animal class enters itself into this hash:

package Sheep; BEGIN { our %::animal_sounds; $::animal_sounds{'baa'} = 'Sheep'; }

And then the make_animal function becomes:

sub make_animal { my ($sound) = @_; foreach my $sounds_like (keys %::animal_sounds) { if ($sound =~ /$sounds_like/) { return ($::animal_sounds{$sounds_like})->new; } } }

This function then would not change when a new Animal is added to the hierarchy.

Can anyone recommend any ways of cleaning this up? Is there a more elegant way of doing what I am trying to do? Without using BEGIN blocks and a global hash perhaps? Or without breaking any strictures?

Is there a CPAN module that does something similiar to this?

-Andrew.


In reply to Factory Pattern by tomazos

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 having a coffee break in the Monastery: (3)
As of 2024-03-29 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found