Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

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

A couple of years ago I wrote a module that tried to implement the Flyweight pattern in perl, and submitted it for Code Review. I had originally planned to release it to CPAN, but reconsidered after I thought more about it. Looking at the code now, there wasn't much advantage to using my module versus just implementing it in modules that need it. Besides, my approach didn't really provide any real encapsulation of the data since it just returned the full data structure to the caller.

However, I still believe it can be a useful approach if data encapsulation is important to you.

In addition to the points Abigail-II identified (no garbage collection after the object goes out of scope, and inability to handle overload stringification) I'd like to add that you're $self doesn't need to be a blessed hash. Since you're only using the blessed hash's address when it's stringified as your object identifier inside the flyweight hash, you could use any type of blessed variable. In this case I'd recommend a simple blessed scalar, which would have less memory consumption and is cleaner IMHO.

Below is a sample class that deals with all three of the issues. It uses overload's StrVal() function to get the scalar's address, bypassing string overloading. (However, I'd only recommend doing this if you actually are using string overloading, for performance reasons):

package Flyweight; use strict; use overload (); { my %flyweight; sub new { my $self = bless \my $scalar => shift; # do other things to set up the object return $self; } sub get_attribute { my $self = shift; my $value = $flyweight{ overload::StrVal($self) }{attribute}; # do something to $value return $value; } sub set_attribute { my $self = shift; my $value = shift; # throw an exception if $value doesn't meet constraints $flyweight{ overload::StrVal($self) }{attribute} = $value; return; } # clean up the object when it goes out of scope sub DESTROY { delete $flyweight{ overload::StrVal(shift) } } } 1;

Dan Kubb, Perl Programmer


In reply to (dkubb) Re: (1) Encapsulation through stringification - a variation on flyweight objects by dkubb
in thread Encapsulation through stringification - a variation on flyweight objects by robartes

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 making s'mores by the fire in the courtyard of the Monastery: (10)
    As of 2014-12-25 05:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (159 votes), past polls