1 # The Singleton module implements the Singleton pattern.
9 # * this ensures that only one instance of Klass lets call it
10 # ``the instance'' can be created.
12 # a,b = Klass.instance, Klass.instance
14 # a.new # NoMethodError - new is private ...
16 # * ``The instance'' is created at instantiation time, in other
17 # words the first call of Klass.instance(), thus
23 # ObjectSpace.each_object(OtherKlass){} # => 0.
25 # * This behavior is preserved under inheritance and cloning.
29 # This is achieved by marking
30 # * Klass.new and Klass.allocate - as private
32 # Providing (or modifying) the class methods
33 # * Klass.inherited(sub_klass) and Klass.clone() -
34 # to ensure that the Singleton pattern is properly
35 # inherited and cloned.
37 # * Klass.instance() - returning ``the instance''. After a
38 # successful self modifying (normally the first) call the
39 # method body is a simple:
41 # def Klass.instance()
42 # return @singleton__instance__
45 # * Klass._load(str) - calling Klass.instance()
47 # * Klass._instantiate?() - returning ``the instance'' or
48 # nil. This hook method puts a second (or nth) thread calling
49 # Klass.instance() on a waiting loop. The return value
50 # signifies the successful completion or premature termination
51 # of the first, or more generally, current "instantiation thread".
54 # The instance method of Singleton are
55 # * clone and dup - raising TypeErrors to prevent cloning or duping
57 # * _dump(depth) - returning the empty string. Marshalling strips
58 # by default all state information, e.g. instance variables and
59 # taint state, from ``the instance''. Providing custom _load(str)
60 # and _dump(depth) hooks allows the (partially) resurrections of
61 # a previous state of ``the instance''.
66 # disable build-in copying methods
68 raise TypeError, "can't clone instance of singleton #{self.class}"
71 raise TypeError, "can't dup instance of singleton #{self.class}"
74 # default marshalling strategy
79 module SingletonClassMethods
80 # properly clone the Singleton pattern - did you know
81 # that duping doesn't copy class methods?
83 Singleton.__init__(super)
92 # ensure that the Singleton pattern is properly inherited
93 def inherited(sub_klass)
95 Singleton.__init__(sub_klass)
101 klass.instance_eval {
102 @singleton__instance__ = nil
103 @singleton__mutex__ = Mutex.new
106 return @singleton__instance__ if @singleton__instance__
107 @singleton__mutex__.synchronize {
108 return @singleton__instance__ if @singleton__instance__
109 @singleton__instance__ = new()
111 @singleton__instance__
118 # extending an object with Singleton is a bad idea
119 undef_method :extend_object
121 def append_features(mod)
122 # help out people counting on transitive mixins
123 unless mod.instance_of?(Class)
124 raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
131 klass.private_class_method :new, :allocate
132 klass.extend SingletonClassMethods
133 Singleton.__init__(klass)
142 def num_of_instances(klass)
143 "#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
146 # The basic and most important example.
148 class SomeSingletonClass
151 puts "There are #{num_of_instances(SomeSingletonClass)}"
153 a = SomeSingletonClass.instance
154 b = SomeSingletonClass.instance # a and b are same object
155 puts "basic test is #{a == b}"
158 SomeSingletonClass.new
159 rescue NoMethodError => mes
165 puts "\nThreaded example with exception and customized #_instantiate?() hook"; p
166 Thread.abort_on_exception = false
168 class Ups < SomeSingletonClass
171 puts "initialize called by thread ##{Thread.current[:i]}"
177 @enter.push Thread.current[:i]
178 while false.equal?(@singleton__instance__)
179 @singleton__mutex__.unlock
181 @singleton__mutex__.lock
183 @leave.push Thread.current[:i]
184 @singleton__instance__
194 raise "boom - thread ##{Thread.current[:i]} failed to create instance"
209 Thread.current[:i] = i
212 rescue RuntimeError => mes
217 puts "Before there were #{num_of_instances(self)}"
219 puts "Now there is #{num_of_instances(self)}"
220 puts "#{@enter.join '; '} was the order of threads entering the waiting loop"
221 puts "#{@leave.join '; '} was the order of threads leaving the waiting loop"
227 # results in message like
228 # Before there were 0 Ups instance(s)
229 # boom - thread #6 failed to create instance
230 # initialize called by thread #3
231 # Now there is 1 Ups instance(s)
232 # 3; 2; 1; 8; 4; 7; 5 was the order of threads entering the waiting loop
233 # 3; 2; 1; 7; 4; 8; 5 was the order of threads leaving the waiting loop
236 puts "\nLets see if class level cloning really works"
241 raise "boom - thread ##{Thread.current[:i]} failed to create instance"
252 puts "\n\n","Customized marshalling"
255 attr_accessor :persist, :die
257 # this strips the @die information from the instance
258 Marshal.dump(@persist,depth)
263 instance.persist = Marshal.load(str)
268 a.persist = ["persist"]
272 stored_state = Marshal.dump(a)
276 b = Marshal.load(stored_state)
278 p a.persist # => ["persist"]
282 puts "\n\nSingleton with overridden default #inherited() hook"
285 def Up.inherited(sub_klass)
286 puts "#{sub_klass} subclasses #{self}"
294 class Down < Middle; end
296 puts "and basic \"Down test\" is #{Down.instance == Down.instance}\n
303 rescue TypeError => mes
304 puts mes #=> Inclusion of the OO-Singleton module in module AModule
308 'aString'.extend Singleton
309 rescue NoMethodError => mes
310 puts mes #=> undefined method `extend_object' for Singleton:Module