Scope-Session

 view release on metacpan or  search on metacpan

lib/Scope/Session/Flyweight.pm  view on Meta::CPAN

work as role

    package Test::Object;
    use Scope::Session::Flyweight acquire => 1;
    sub identifier {
        my ( $class, %args ) = @_;
        return $args{id};
    }
    sub new {
        my ( $class, %args ) = @_;
        return bless {%args} => $class;
    }
    my $flyweight  = Test::Object->acquire( id => 10 );
    my $flyweight2 = Test::Object->acquire( id => 10 );

=head1 METHODS

=head2 acquire

=head1 TARGET CLASS

t/03_flyweight.t  view on Meta::CPAN

use Test::MockObject;
use Scalar::Util qw/refaddr/;
BEGIN{
    use_ok('Scope::Session::Flyweight');
}

{
    package Test::Object;
    sub new {
        my ( $class, %args ) = @_;
        return bless {%args} => $class;
    }
    sub identifier {
        my ( $class, %args ) = @_;
        return $args{id};
    }
}
{
    package Test::Object::Singleton::Flyweighted;
    use Scope::Session::Flyweight acquire => 1;
    sub identifier {
        my ( $class, %args ) = @_;
        return $args{id};
    }

    sub new {
        my ( $class, %args ) = @_;
        return bless {%args} => $class;
    }

}

Scope::Session::start {
    my $flyweight
        = Scope::Session::Flyweight->acquire( q|Test::Object| => ( id => 10 ) );
    my $flyweight2
        = Scope::Session::Flyweight->acquire( q|Test::Object| => ( id => 10 ) );
    ::ok $flyweight ;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.051 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-9f2165ba459b )