Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

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



  • 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 examining the Monastery: (3)
As of 2024-03-29 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found