water has asked for the wisdom of the Perl Monks concerning the following question:
"Factory" in quotes, as the chatterbox denizens quickly set me straight and let me know I didn't have the concept of a factory object right. Thanks all for your help.
I'm still curious about the idea, and wanted to ask the OO design gurus here in SOPW.
Here's my problem/idea:
I was seeking an object that would construct itself as the right type, depending on constructor parameters.
A factory class produces widget objects; it doesn't become a widget. In contrast, I was thinking about something like a seed: a seed doesn't produce plants, it turns itself into a plant.
Here's some pseudo-code:
My question: does this approach (a constructor that returns something of the right type depending on its parameters) have a name?# untested code # # factory approach my $factory = AnimalFoodFactory->new; my $food_object = AnimalFoodFactory->prepare_correct_chow( animal=> 'lion'); # now ref $food_object eq 'LionFood' # "seed" (?) approach my $food_object = AnimalFoodPreparer->new(animal=> 'lion'); # now ref $food_object eq 'LionFood'
And does the approach make sense, or is Bad Practice?
Curious --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: OO: Factory vs. "Seed" (?) pattern
by dragonchild (Archbishop) on May 14, 2005 at 03:38 UTC | |
|
Re: OO: Factory vs. "Seed" (?) pattern
by lachoy (Parson) on May 14, 2005 at 09:54 UTC | |
|
Re: OO: Factory vs. "Seed" (?) pattern
by gaal (Parson) on May 14, 2005 at 09:57 UTC | |
by water (Deacon) on May 14, 2005 at 10:33 UTC | |
by gaal (Parson) on May 14, 2005 at 10:48 UTC |