releasing/cbrtools/perl/Class/Singleton.pm
author Zheng Shen <zheng.shen@nokia.com>
Wed, 27 Oct 2010 19:16:18 +0800
changeset 663 8e27d440923e
parent 602 3145852acc89
permissions -rw-r--r--
revert to 7c11c3d8d025
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
#============================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# Class::Singleton.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# Implementation of a "singleton" module which ensures that a class has
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# only one instance and provides global access to it.  For a description 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# of the Singleton class, see "Design Patterns", Gamma et al, Addison-
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
# Wesley, 1995, ISBN 0-201-63361-2
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# Written by Andy Wardley <abw@cre.canon.co.uk>
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# Copyright (C) 1998 Canon Research Centre Europe Ltd.  All Rights Reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
#----------------------------------------------------------------------------
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
# $Id: Singleton.pm,v 1.3 1999/01/19 15:57:43 abw Exp $
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
#============================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
package Class::Singleton;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
require 5.004;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
use vars qw( $RCS_ID $VERSION );
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
$VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
$RCS_ID  = q$Id: Singleton.pm,v 1.3 1999/01/19 15:57:43 abw Exp $;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
#========================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
#                      -----  PUBLIC METHODS -----
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
#========================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
#========================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
# instance()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
# Module constructor.  Creates an Class::Singleton (or derivative) instance 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
# if one doesn't already exist.  The instance reference is stored in the
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
# _instance variable of the $class package.  This means that classes 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
# derived from Class::Singleton will have the variables defined in *THEIR*
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
# package, rather than the Class::Singleton package.  The impact of this is
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
# that you can create any number of classes derived from Class::Singleton
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
# and create a single instance of each one.  If the _instance variable
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
# was stored in the Class::Singleton package, you could only instantiate 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
# *ONE* object of *ANY* class derived from Class::Singleton.  The first
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
# time the instance is created, the _new_instance() constructor is called 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
# which simply returns a reference to a blessed hash.  This can be 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
# overloaded for custom constructors.  Any addtional parameters passed to 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
# instance() are forwarded to _new_instance().
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
# Returns a reference to the existing, or a newly created Class::Singleton
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
# object.  If the _new_instance() method returns an undefined value
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
# then the constructer is deemed to have failed.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
#========================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
sub instance {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
    my $class = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
    # get a reference to the _instance variable in the $class package 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
    no strict 'refs';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
    my $instance = \${ "$class\::_instance" };
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
    defined $$instance
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
	? $$instance
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
	: ($$instance = $class->_new_instance(@_));
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
#========================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
# _new_instance(...)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
# Simple constructor which returns a hash reference blessed into the 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
# current class.  May be overloaded to create non-hash objects or 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
# handle any specific initialisation required.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
# Returns a reference to the blessed hash.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
#========================================================================
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
sub _new_instance {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
    bless { }, $_[0];
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
__END__
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
Class::Singleton - Implementation of a "Singleton" class 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
=head1 SYNOPSIS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
    use Class::Singleton;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
    my $one = Class::Singleton->instance();   # returns a new instance
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
    my $two = Class::Singleton->instance();   # returns same instance
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
=head1 DESCRIPTION
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
This is the Class::Singleton module.  A Singleton describes an object class
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
that can have only one instance in any system.  An example of a Singleton
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
might be a print spooler or system registry.  This module implements a
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
Singleton class from which other classes can be derived.  By itself, the
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
Class::Singleton module does very little other than manage the instantiation
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
of a single object.  In deriving a class from Class::Singleton, your module 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
will inherit the Singleton instantiation method and can implement whatever
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
specific functionality is required.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
For a description and discussion of the Singleton class, see 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
"Design Patterns", Gamma et al, Addison-Wesley, 1995, ISBN 0-201-63361-2.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
=head1 PREREQUISITES
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
Class::Singleton requires Perl version 5.004 or later.  If you have an older 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
version of Perl, please upgrade to latest version.  Perl 5.004 is known 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
to be stable and includes new features and defect fixes over previous
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
versions.  Perl itself is available from your nearest CPAN site (see
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
INSTALLATION below).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
=head1 INSTALLATION
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
The Class::Singleton module is available from CPAN. As the 'perlmod' man
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
page explains:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
    CPAN stands for the Comprehensive Perl Archive Network.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
    This is a globally replicated collection of all known Perl
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
    materials, including hundreds of unbunded modules.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
    [...]
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
    For an up-to-date listing of CPAN sites, see
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
    http://www.perl.com/perl/ or ftp://ftp.perl.com/perl/ .
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
The module is available in the following directories:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
    /modules/by-module/Class/Class-Singleton-<version>.tar.gz
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
    /authors/id/ABW/Class-Singleton-<version>.tar.gz
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
For the latest information on Class-Singleton or to download the latest
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   149
pre-release/beta version of the module, consult the definitive reference:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   150
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   151
    http://www.kfs.org/~abw/perl/
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   152
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   153
Class::Singleton is distributed as a single gzipped tar archive file:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   154
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   155
    Class-Singleton-<version>.tar.gz
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   156
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   157
Note that "<version>" represents the current version number, of the 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   158
form "1.23".  See L<REVISION> below to determine the current version 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   159
number for Class::Singleton.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   160
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   161
Unpack the archive to create an installation directory:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   162
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   163
    gunzip Class-Singleton-<version>.tar.gz
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   164
    tar xvf Class-Singleton-<version>.tar
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   165
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   166
'cd' into that directory, make, test and install the module:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   167
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   168
    cd Class-Singleton-<version>
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   169
    perl Makefile.PL
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   170
    make
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   171
    make test
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   172
    make install
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   173
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   174
The 'make install' will install the module on your system.  You may need 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   175
root access to perform this task.  If you install the module in a local 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   176
directory (for example, by executing "perl Makefile.PL LIB=~/lib" in the 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   177
above - see C<perldoc MakeMaker> for full details), you will need to ensure 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   178
that the PERL5LIB environment variable is set to include the location, or 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   179
add a line to your scripts explicitly naming the library location:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   180
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   181
    use lib '/local/path/to/lib';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   182
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   183
=head1 USING THE CLASS::SINGLETON MODULE
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   184
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   185
To import and use the Class::Singleton module the following line should 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   186
appear in your Perl script:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   187
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   188
    use Class::Singleton;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   189
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   190
The instance() method is used to create a new Class::Singleton instance, 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   191
or return a reference to an existing instance.  Using this method, it
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   192
is only possible to have a single instance of the class in any system.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   193
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   194
    my $highlander = Class::Singleton->instance();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   195
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   196
Assuming that no Class::Singleton object currently exists, this first
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   197
call to instance() will create a new Class::Singleton and return a reference
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   198
to it.  Future invocations of instance() will return the same reference.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   199
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   200
    my $macleod    = Class::Singleton->instance();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   201
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   202
In the above example, both $highlander and $macleod contain the same
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   203
reference to a Class::Singleton instance.  There can be only one.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   204
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   205
=head1 DERIVING SINGLETON CLASSES
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   206
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   207
A module class may be derived from Class::Singleton and will inherit the 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   208
instance() method that correctly instantiates only one object.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   209
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   210
    package PrintSpooler;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   211
    use vars qw(@ISA);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   212
    @ISA = qw(Class::Singleton);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   213
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   214
    # derived class specific code
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   215
    sub submit_job {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   216
        ...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   217
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   218
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   219
    sub cancel_job {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   220
        ...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   221
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   222
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   223
The PrintSpooler class defined above could be used as follows:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   224
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   225
    use PrintSpooler;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   226
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   227
    my $spooler = PrintSpooler->instance();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   228
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   229
    $spooler->submit_job(...);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   230
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   231
The instance() method calls the _new_instance() constructor method the 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   232
first and only time a new instance is created.  All parameters passed to 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   233
the instance() method are forwarded to _new_instance().  In the base class
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   234
this method returns a blessed reference to an empty hash array.  Derived 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   235
classes may redefine it to provide specific object initialisation or change
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   236
the underlying object type (to a list reference, for example).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   237
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   238
    package MyApp::Database;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   239
    use vars qw( $ERROR );
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   240
    use base qw( Class::Singleton );
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   241
    use DBI;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   242
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   243
    $ERROR = '';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   244
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   245
    # this only gets called the first time instance() is called
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   246
    sub _new_instance {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   247
	my $class = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   248
	my $self  = bless { }, $class;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   249
	my $db    = shift || "myappdb";    
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   250
	my $host  = shift || "localhost";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   251
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   252
	unless (defined ($self->{ DB } 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   253
			 = DBI->connect("DBI:mSQL:$db:$host"))) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   254
	    $ERROR = "Cannot connect to database: $DBI::errstr\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   255
	    # return failure;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   256
	    return undef;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   257
	}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   258
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   259
	# any other initialisation...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   260
	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   261
	# return sucess
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   262
	$self;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   263
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   264
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   265
The above example might be used as follows:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   266
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   267
    use MyApp::Database;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   268
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   269
    # first use - database gets initialised
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   270
    my $database = MyApp::Database->instance();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   271
    die $MyApp::Database::ERROR unless defined $database;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   272
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   273
Some time later on in a module far, far away...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   274
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   275
    package MyApp::FooBar
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   276
    use MyApp::Database;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   277
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   278
    sub new {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   279
	# usual stuff...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   280
	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   281
	# this FooBar object needs access to the database; the Singleton
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   282
	# approach gives a nice wrapper around global variables.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   283
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   284
	# subsequent use - existing instance gets returned
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   285
	my $database = MyApp::Database->instance();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   286
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   287
	# the new() isn't called if an instance already exists,
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   288
	# so the above constructor shouldn't fail, but we check
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   289
	# anyway.  One day things might change and this could be the
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   290
	# first call to instance()...  
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   291
	die $MyAppDatabase::ERROR unless defined $database;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   292
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   293
	# more stuff...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   294
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   295
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   296
The Class::Singleton instance() method uses a package variable to store a
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   297
reference to any existing instance of the object.  This variable, 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   298
"_instance", is coerced into the derived class package rather than
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   299
the base class package.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   300
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   301
Thus, in the MyApp::Database example above, the instance variable would
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   302
be:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   303
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   304
    $MyApp::Database::_instance;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   305
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   306
This allows different classes to be derived from Class::Singleton that 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   307
can co-exist in the same system, while still allowing only one instance
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   308
of any one class to exists.  For example, it would be possible to 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   309
derive both 'PrintSpooler' and 'MyApp::Database' from Class::Singleton and
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   310
have a single instance of I<each> in a system, rather than a single 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   311
instance of I<either>.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   312
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   313
=head1 AUTHOR
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   314
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   315
Andy Wardley, C<E<lt>abw@cre.canon.co.ukE<gt>>
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   316
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   317
Web Technology Group, Canon Research Centre Europe Ltd.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   318
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   319
Thanks to Andreas Koenig C<E<lt>andreas.koenig@anima.deE<gt>> for providing
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   320
some significant speedup patches and other ideas.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   321
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   322
=head1 REVISION
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   323
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   324
$Revision: 1.3 $
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   325
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   326
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   327
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   328
Copyright (C) 1998 Canon Research Centre Europe Ltd.  All Rights Reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   329
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   330
This module is free software; you can redistribute it and/or modify it under 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   331
the term of the Perl Artistic License.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   332
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   333
=head1 SEE ALSO
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   334
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   335
=over 4
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   336
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   337
=item Canon Research Centre Europe Perl Pages
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   338
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   339
http://www.cre.canon.co.uk/perl/
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   340
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   341
=item The Author's Home Page
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   342
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   343
http://www.kfs.org/~abw/
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   344
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   345
=item Design Patterns
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   346
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   347
Class::Singleton is an implementation of the Singleton class described in 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   348
"Design Patterns", Gamma et al, Addison-Wesley, 1995, ISBN 0-201-63361-2
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   349
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   350
=back
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   351
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   352
=cut