Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Interesting idea.

I share your concern over the "clone" method. I would be inclined to name the two main methods "instantiate" and "access". So your example would look like:

package MyPackage; use strict; use Class::Flyweight; my $flyweight = Class::Flyweight->new(); sub new { return $flyweight->instantiate(bless {}, shift); } sub get_name { my $self = $flyweight->access(shift); return $self->{name}; }
etc. I would also export a private DESTROY method to do the cleanup. There should be no reason for classes using this to rewrite DESTROY all of the time by default.

But while I like the idea, I look at it and I have to wonder what you have really added to what you get from hand-rolling:

package MyPackage; use strict; my %instance; sub new { my $class = shift; my $obj = {}; my $name = "$obj"; $instance{$name} = $obj; return bless $name, class; } sub get_name { my $self = $instance{shift(@_)}; return $self->{name}; }
etc. Well you have gained something, but I am not sure that the overhead of adding a method call to every method call is enough for me.

The direction that I would be inclined to go instead is to make your encapsulation weaker. Instead of exporting methods that can be used to implement a flyweight, accept a hash of package translations and implement each class in terms of the translated one, with a shared lexical scope. That way the flyweight is implemented by having an autogenerated class proxy for a private implementation. This way someone could develop with encapsulation, and then before making the code production could change their package names and drop the flyweight protection for speed.

This would involve writing and evaling code which would wrap methods with AUTOLOAD. The protection would be less (after all someone could easily take a look at your actual package names). But it would still provide something and it now is clear to me what value is added over handrolling my own flyweight implementation (better performance in production).

A note. The question was asked of how to handle inheritance. Well yours handles that by allowing you to explicitly copy the private flyweight object to each inherited class. My suggestion would take more thought on your part. Perhaps the private packages when you set up the flyweight mapping will get treated specially, so the only thing that changes when you go to production is that you eliminate the flyweight and change package names.

As for your other question, I think this is neat but I am not sure that it is solving a pressing problem for me. So I probably wouldn't use it. But I would put this in my list of neat things to keep in mind just in case, even though I don't have a specific use in mind right now...


In reply to Re (tilly) 1: Class::Flyweight - implement the flyweight pattern in OO perl by tilly
in thread Class::Flyweight - implement the flyweight pattern in OO perl by dkubb

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2014-08-30 11:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (293 votes), past polls