Clear questions and runnable code
get the best and fastest answer
OO: Building an object of the right type based on a parameterby rkg (Hermit)
|on Mar 11, 2004 at 03:29 UTC ( #335696=perlquestion: print w/replies, xml )||Need Help??|
rkg has asked for the wisdom of the Perl Monks concerning the following question:
Hi. rkg again, back with another OO question:
I have set of Client objects. A Client is a simple Class::DBI wrapper from a single client sql table, with a handful of extra methods.
I have a InvoiceCalculator object, which upon construction takes a client object as an argument. Probably I could push this up into the Client class as ->calculate_invoice, but I haven't.
Certain specific instances of Clients (say clients A1, A2 and C) get different business logic for their invoices. Fine -- I have InvoiceCalulator::Generic, and subclass it with the changes as InvoiceCalculator::A, InvoiceCalculator::C, etc.
Here's my question: when I go to create an InvoiceCalculator (be it generic, or A, or C), I need to examine the client to know what kind to construct. Having a big case statement ("if client = A1 or A2 make a ::A, if client = C make a ::C, etc:) feels wrong.
The other thought I had was to add the proper InvoiceCalculator class as field in the Client object.
This too feels wrong: something seems strange about storing a class name (eg storing code) in a field. Feels brittle, perhaps.
Suggestions? What's the right approach to build an IC object based on the specifics of the client object?
Apologies if the post isn't fully clear; I've been suffering a bit of incoherence recently.
Thanks for any advice --
Back to Seekers of Perl Wisdom