Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Having a big case statement ("if client = A1 or A2 make a ::A, if client = C make a ::C, etc:) feels wrong.

Good. That means you have the OO mindset. ;-)

Whenever you find yourself thinking that you need a case statement that switches on an object's class, you should probably move the switched code into the objects themselves, as kappa suggested. Unfortunately, kappa's suggestion creates a circular dependency between the Client and InvoiceCalculator classes.

That's not a disaster as design problems go (at least not in Perl... C++ is another story), but it's a good idea to get rid of circular dependencies whenever possible. In this case, you have two options:

  1. promote the creation process to a higher class, or
  2. demote the shared information to a lower class.

Promoting creation to a higher class means doing something like so:

package ClientFactory; sub genericClient { my $client = new Client(); my $calculator = new InvoiceCalculator ($client); $client->setCalculator ($calculator); return ($client); } package main; my $client = ClientFactory::genericClient(); my $calculator = $client->getCalculator();

Every Client is associated with the right kind of InvoiceCalculator right from the start, so all we have to do is ask for it when we need it. Neither the Client nor the InvoiceCalculator actually creates the other, so the circular dependency is gone. We do still have two classes carrying references to each other, though, and that's a bit clunky.

Demoting the shared information means creating a data storage class that both the Client and InvoiceCalculator classes can share:

package Storage; sub new { return (bless { 'key 1' => 'default 1', 'key 2' => 'default 2', }, shift); } package InvoiceCalculator; sub new { my $O = bless {}, shift; $O->{'data'} = shift; return ($O); } sub calculate { my $O = shift; for my $k (sort keys %{ $O->{'data'} }) { printf ("%s -> %s\n", $k, $O->{'data'}->{ $k }); } return; } package Client; sub new { my $O = bless {}, shift; $O->{'data'} = new Storage (); return ($O); } sub getCalculator { my $O = shift; return (new InvoiceCalculator ($O->{'data'})); } package main; my $client = new Client; my $calc = $client->getCalculator(); $calc->calculate();

This costs a few extra keystrokes to work with the embedded Storage object, especially if we decide to be really pure and use set() and get() methods to move data back and forth. Being pure does have its value, though, since the access methods give us a place to put sanity-and-hygiene code for the stored values. We can also use the more relaxed approach shown in InvoiceCalculator::calculate(), though, and twiddle the values directly. If we really resent the extra keystrokes, we can use a temporary reference:

sub InvoiceCalculator::calculate { my $O = shift; my $data = $O->{'data'}; for my $k (sort keys %$data) { printf ("%s -> %s\n", $k, $data->{ $k }); } return; }

In reply to Re: OO: Building an object of the right type based on a parameter by mstone
in thread 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-24 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found