Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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 ;) ?


In reply to Encapsulation through stringification - a variation on flyweight objects by robartes

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (7)
    As of 2018-06-19 02:03 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (111 votes). Check out past polls.