Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Most of us are familiar with the term 'flyweight objects' in Perl OO speak, which refers to objects where the blessed thingy is a scalar that is the index into something (a hash or array) containing the object's attributes. This container is lexically scoped to a bare block in the package implementing the object's class, thus providing fairly strong encapsulation.

One way of subverting this encapsulation is a basic 'identity switch' job. A flyweight object does not carry its own attributes around, but it does carry an index around. This index can be modified to take on the identity of some other object in the same class. One way of getting around this is simply making the index harder to guess by randomizing it.

While idling away at the keyboard tonight, I came up with what might be another way of strenghtening the encapsulation in flyweight objects: instead of having the object carrying an index around, have the object be its own index:

package Camel; { my %camels; sub new { my $class=shift; my %args=@_; my $self={}; bless $self, $class; my $key=qq/$self/; $camels{$key}={ %args }; return $self; } } 1;
As you see, the index into the attribute container is a stringified version of the blessed reference itself. The blessed reference is just an empty anonymous hash, so there's no longer an index to play around with. The object is empty - to get at the attributes, you would have to write accessor methods inside the scoping block of package Camel. Encapsulation!

Am I missing something here? Can anybody poke some holes in this? And if not, can anybody come up with a nice name for this little gimmick ;) ?

CU
Robartes-


In reply to 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 pondering the Monastery: (5)
    As of 2014-08-21 22:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (144 votes), past polls