tomazos has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Factory Pattern
by merlyn (Sage) on Jun 21, 2006 at 10:29 UTC | |
Re: Factory Pattern
by Corion (Patriarch) on Jun 21, 2006 at 10:40 UTC | |
Re: Factory Pattern
by adrianh (Chancellor) on Jun 22, 2006 at 07:12 UTC |