Class::StrongSingleton

Class::StrongSingleton is a stronger and more secure Singleton base class.
Download

Class::StrongSingleton Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Stevan Little
  • Publisher web site:
  • http://search.cpan.org/~stevan/

Class::StrongSingleton Tags


Class::StrongSingleton Description

Class::StrongSingleton is a stronger and more secure Singleton base class. Class::StrongSingleton is a stronger and more secure Singleton base class.SYNOPSIS package My::Singleton::Class; use base qw(Class::StrongSingleton); sub new { my ($class, %my_params) = @_; # create our object instance my $instance = { %my_params }; bless($instance, $class); # and initialize it as a singleton $instance->_init_StrongSingleton(); return $instance; } 1; # later in your code ... # create the first instance of our class my $instance = My::Singleton::Class->new(param => "value"); # try to create a 'new' one again, and # you end up with the same instance, not # a new one my $instance2 = My::Singleton::Class->new(param => "other value"); # calling 'instance' returns the singleton # instance expected my $instance3 = My::Singleton::Class->instance(); # although rarely needed, if you have to # you can destroy the singleton # either through the instance $instance->DESTROY(); # or through the class My::Singleton::Class->DESTROY(); # of course, this is assuming you # did not override DESTORY yourself # Also calling 'instance' before calling 'new' # will returns a new singleton instance my $instance = My::Singleton::Class->instance(); This module is an alternative to Class::Singleton and Class::WeakSingleton, and provides a more secure Singleton class in that it takes steps to prevent the possibility of accidental creation of multiple instances and/or the overwriting of existsing Singleton instances. For a detailed comparison please see the "SEE ALSO" section.Here is a description of how it all works. First, the user creates the first Singleton instance of the class in the normal way. my $obj = My::Singleton::Class->new("variable", "parameter");This instance is then stored inside a lexically scoped variable within the Class::StrongSingleton package. This prevents the variable from being accessed by anything but methods from the Class::StrongSingleton package. At this point as well, the new method to the class is overridden so that it will always return the Singleton instance. This prevents any accidental overwriting of the Singleton instance. This means that any of the follow lines of code all produce the same instance: my $instance = $obj->instance(); my $instance = My::Singleton::Class->instance(); my $instance = $obj->new(); my $instance = My::Singleton::Class->new();Personally, I see this an an improvement over the usual Gang of Four style Singletons which discourages the use of the new method entirely. Through this method, a user can be able to use the Singleton class in a normal way, not having to know it's actually a Singleton. This can be handy if your design changes and you no longer need the class as a Singleton. Requirements: · Perl


Class::StrongSingleton Related Software