"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 --
In reply to OO: Factory vs. "Seed" (?) pattern by water
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |