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

comment on

( [id://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":



  • 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 studying the Monastery: (5)
As of 2024-03-29 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found