perlquestion
rkg
Hi. [rkg] again, back with another OO question:<p>
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. <p>
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. <p>
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. <p>
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. <p> The other thought I had was to add the proper InvoiceCalculator class as field in the Client object. <code>
# untested
my $InvoiceCalculatorClass = $client->InvoiceCalculator;
my $ic = $InvoiceCalculatorClass->new(client=>$client);
</code>
This too feels wrong: something seems strange about storing a class name (eg storing code) in a field. Feels brittle, perhaps.
<p>
Suggestions? What's the right approach to build an IC object based on the specifics of the client object?
<p>
Apologies if the post isn't fully clear; I've been suffering a bit of incoherence recently.
<p>
Thanks for any advice --
<p>
[rkg]