in reply to Large datastructure caching
Basically, a flyweight object is a blessed scalar which contains an index into an array of references to something (sound familiar? :) ). So, instead of passing $id around, why don't you make $id your object by blessing it, and put references the hashes containing {string} keys in an array, indexed by $id. Your constructor can then either take no argument, which creates a new entry in the array, or take an id to give you a handle on the object with that id.
Something like this:
Note that this code is untested, and that I used hash style argument passing (new({id=>11, string=>"camel"})) in the constructor to account for the optional presence of id.use strict; package Demo; my @handles; sub new { my $thingy=shift; my $args=shift; my $id=$args->{'id'}; # Note that $args->{'id'} is autovivified here, but # this is no problem as we're testing for definedness # further on my $class=ref($thingy)||$thingy; unless (defined $id) {$id=scalar @handles}; my $self=bless $id, $class; $handle[$self]={string => $args->{'string'}}; return $self; }
I also have no idea how this plays with SOAP, as I have no experience on that matter.
Your accessor functions then do no have to have $id passed to them, they can just use $self. This will gain you a minor improvement in elegance, for what it's worth :)
CU
Robartes-
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Large datastructure caching
by rdfield (Priest) on Nov 18, 2002 at 10:01 UTC | |
|
Re: Re: Large datastructure caching
by rdfield (Priest) on Nov 16, 2002 at 15:14 UTC |