The class is quite useful when dealing with a single facility, but I would like to make it more useful. Rather than having to know a unique id, or the actual values that will be used by the object, I'd like to be able to pass in (for example) a city and a state, and get an arrayref of Facility objects for every facility in that city and state.
Now, I have noticed that the class names for many classes in Java match the pattern <Classname>Factory, and I thought "Aha! I could create a FacilityFactory class, which might have the following usage:"
my $factory = new FacilityFactory(
facility_type => 'NursingHome',
city => 'Birmingham',
state => 'AL'
);
foreach my $facility ( @$factory ) {
print $facility->get_name;
}
FacilityFactory would know how to translate its parameters into valid SQL, query the database, and return an arrayref of instantiated Facility objects.
Well, okay that would work, but then I decided to go see if this was actually consistent with the <Classname>Factory pattern that I have seen used in Java. And wouldn't you know, it isn't. The Factory pattern is used when you can't know until runtime which type of class you need to instantiate. The Factory class encapsulates the logic for selecting one of these child clases, is implemented as an abstract base class. Kind of a different beast than what I was attempting.
So here's my question. What is the best way to crack this nut? Here are my options as I see them:
Thanks for any comments.
Class::DBI (Score:2, Informative)
Strangely enough I rambled on about it here [perlmonks.com] last night on Perlmonks [perlmonks.com].