NAME
Aspect::Singleton - Modular aspect to force singleton behavior on a class
SYNOPSIS
use Aspect::Singleton;
my $s = Aspect::Singleton->new('Foo');
my $f1 = Foo->new;
my $f2 = Foo->new;
# now $f1 and $f2 refer to the same object
DESCRIPTION
This class implements a modular aspect that forces singleton behavior on a class.
METHODS
This class inherits from Aspect::Modular
. In addition, it implements and/or overrides the following methods:
define(STRING)
-
Creates and enables advice that implements the singleton behavior for the given class. The class can only be specified as a string.
class([class])
-
Gets, if called without an argument, or sets, if called with an argument, the package name of the class to be affected. It is set automatically by
define()
; setting it afterwards has no effect. enable()
-
Calls the superclass's
enable()
method with the class name as an argument, which in effect restricts the search for possible join points to that class only, thereby saving time. disable()
-
Calls the superclass's
disable()
method with the class name as an argument, which in effect restricts the search for possible join points to that class only, thereby saving time.
INTERNALS
This aspect consists of two pieces of advice for each affected class:
When calling the constructor, check whether we hold its object in cache. If so, return it. If not, let it go ahead and construct one.
When exiting from the constructor, which can only happen once when the singleton object is created, take the newly created object and put it into the cache.
BUGS
None known so far. If you find any bugs or oddities, please do inform the author.
AUTHOR
Marcel Grunauer, <marcel@codewerk.com>
COPYRIGHT
Copyright 2001 Marcel Grunauer. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
perl(1), Aspect::Intro(3pm), Aspect::Overview(3pm).