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.

# untested my $InvoiceCalculatorClass = $client->InvoiceCalculator; my $ic = $InvoiceCalculatorClass->new(client=>$client);
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 --

rkg


In reply to OO: Building an object of the right type based on a parameter by rkg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.