Index: xotcl/library/patterns/Singleton.xotcl =================================================================== diff -u -r5ce5a10c82bc948f50fc4542f844dcd50de1eae3 -r435b41481fb51bf000ebe736d8574fefbeec1710 --- xotcl/library/patterns/Singleton.xotcl (.../Singleton.xotcl) (revision 5ce5a10c82bc948f50fc4542f844dcd50de1eae3) +++ xotcl/library/patterns/Singleton.xotcl (.../Singleton.xotcl) (revision 435b41481fb51bf000ebe736d8574fefbeec1710) @@ -1,76 +1,84 @@ -# $Id: Singleton.xotcl,v 1.2 2004/07/03 21:19:39 neumann Exp $ +# $Id: Singleton.xotcl,v 1.3 2005/09/09 21:07:23 neumann Exp $ + package provide xotcl::pattern::singleton 0.8 +package require XOTcl -Class SingletonBase -SingletonBase instproc getInstance args { - my instvar _instance - if {[info exists _instance]} { - return $_instance - } - return "" -} +namespace eval ::xotcl::pattern::singleton { + namespace import ::xotcl::* + Class SingletonBase + SingletonBase instproc getInstance args { + my instvar _instance + if {[info exists _instance]} { + return $_instance + } + return "" + } -# -# A simple pattern mixin that makes a class to a non-specializable singleton -# -Class NonSpecializableSingleton -superclass SingletonBase -NonSpecializableSingleton instproc create args { - my instvar _instance - if {![info exists _instance]} { - set _instance [self] - next - } - return $_instance -} + # + # A simple pattern mixin that makes a class to a non-specializable singleton + # + Class NonSpecializableSingleton -superclass SingletonBase -NonSpecializableSingleton instproc getInstance {} { - if {[info exists _instance]} { - my instvar _instance - return $_instance - } - return "" -} + NonSpecializableSingleton instproc create args { + my instvar _instance + if {![info exists _instance]} { + set _instance [self] + next + } + return $_instance + } -# -# Specializable Singleton -# -Class Singleton -superclass {SingletonBase Class} -Singleton instproc singletonFilter args { - switch -exact [self calledproc] { - init { - set registrationclass [lindex [self filterreg] 0] - $registrationclass instvar _instance - if {![info exists _instance]} { - set _instance [self] - next - } else { - my destroy - } - return $_instance + NonSpecializableSingleton instproc getInstance {} { + if {[info exists _instance]} { + my instvar _instance + return $_instance + } + return "" } - default { - return [next] + + # + # Specializable Singleton + # + Class Singleton -superclass {SingletonBase Class} + Singleton instproc singletonFilter args { + switch -exact [self calledproc] { + init { + set registrationclass [lindex [self filterreg] 0] + $registrationclass instvar _instance + if {![info exists _instance]} { + set _instance [self] + next + } else { + my destroy + } + return $_instance + } + default { + return [next] + } + } } - } -} -Singleton instproc init args { - my instfilter add singletonFilter - # - # specialized singletons have to look up the singleton class - # first - Class instproc getInstance {} { - foreach sc [my info superclass] { - if {[$sc info class] == "::Singleton"} { - return [$sc getInstance] - } else { - return "" - } + Singleton instproc init args { + my instfilter add singletonFilter + # + # specialized singletons have to look up the singleton class + # first + Class instproc getInstance {} { + foreach sc [my info superclass] { + if {[$sc info class] == "::Singleton"} { + return [$sc getInstance] + } else { + return "" + } + } + } + next } - } - next + + namespace export SingletonBase NonSpecializableSingleton Singleton } - +namespace import ::xotcl::pattern::singleton::*