1552
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Object subclass:#Singleton
|
|
4 |
instanceVariableNames:''
|
|
5 |
classVariableNames:'Lock'
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'System-Support'
|
|
8 |
!
|
|
9 |
|
|
10 |
Singleton class instanceVariableNames:'theOnlyInstance'
|
|
11 |
|
|
12 |
"
|
|
13 |
No other class instance variables are inherited by this class.
|
|
14 |
"
|
|
15 |
!
|
|
16 |
|
|
17 |
!Singleton class methodsFor:'documentation'!
|
|
18 |
|
|
19 |
documentation
|
|
20 |
"
|
|
21 |
Subclasses of Singleton have only a single instance.
|
|
22 |
The instance creation methods ensure that there is only one instance.
|
|
23 |
|
|
24 |
Singletons that would have inherited from Object could inherit from
|
|
25 |
Singleton. For Singletons that do not inherit from Object,
|
|
26 |
you have to copy the Singleton's code to your class.
|
|
27 |
|
|
28 |
[author:]
|
|
29 |
Stefan Vogel (stefan@zwerg)
|
|
30 |
|
|
31 |
[class instance variables:]
|
|
32 |
theOnlyInstance Object The only instance of this class
|
|
33 |
|
|
34 |
[class variables:]
|
|
35 |
Lock RecursionLock A global lock to protect agains races during instance creation
|
|
36 |
"
|
|
37 |
! !
|
|
38 |
|
|
39 |
!Singleton class methodsFor:'initialization'!
|
|
40 |
|
|
41 |
initialize
|
|
42 |
Lock := RecursionLock new name:#Singleton
|
|
43 |
! !
|
|
44 |
|
|
45 |
!Singleton class methodsFor:'instance creation'!
|
|
46 |
|
|
47 |
basicNew
|
|
48 |
"allocate a singleton in a thread-safe way.
|
|
49 |
Do lazy locking here"
|
|
50 |
|
|
51 |
theOnlyInstance isNil ifTrue:[
|
|
52 |
Lock critical:[
|
|
53 |
theOnlyInstance isNil ifTrue:[
|
|
54 |
theOnlyInstance := super basicNew.
|
|
55 |
].
|
|
56 |
]
|
|
57 |
].
|
|
58 |
^ theOnlyInstance.
|
|
59 |
!
|
|
60 |
|
|
61 |
basicNew:anInteger
|
|
62 |
"allocate a singleton in a thread-safe way.
|
|
63 |
Do lazy locking here"
|
|
64 |
|
|
65 |
theOnlyInstance isNil ifTrue:[
|
|
66 |
Lock critical:[
|
|
67 |
theOnlyInstance isNil ifTrue:[
|
|
68 |
theOnlyInstance := super basicNew:anInteger.
|
|
69 |
].
|
|
70 |
]
|
|
71 |
].
|
|
72 |
^ theOnlyInstance.
|
|
73 |
! !
|
|
74 |
|
|
75 |
!Singleton class methodsFor:'accessing'!
|
|
76 |
|
1643
|
77 |
singletonLock
|
|
78 |
"can be used by other classes that are not subclasses of Singleton"
|
|
79 |
|
|
80 |
^ Lock
|
|
81 |
!
|
|
82 |
|
1552
|
83 |
theOnlyInstance
|
|
84 |
^ theOnlyInstance
|
|
85 |
! !
|
|
86 |
|
|
87 |
!Singleton class methodsFor:'documentation'!
|
|
88 |
|
|
89 |
version
|
1643
|
90 |
^ '$Header: /cvs/stx/stx/libbasic2/Singleton.st,v 1.2 2006-06-29 08:20:52 stefan Exp $'
|
1552
|
91 |
! !
|
|
92 |
|
|
93 |
Singleton initialize!
|