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}"
76 # default marshalling strategy
81 module SingletonClassMethods
82 # properly clone the Singleton pattern - did you know
83 # that duping doesn't copy class methods?
85 Singleton.__init__(super)
90 # ensure that the Singleton pattern is properly inherited
91 def inherited(sub_klass)
93 Singleton.__init__(sub_klass)
103 klass.instance_eval {
104 @singleton__instance__ = nil
105 @singleton__mutex__ = Mutex.new
108 return @singleton__instance__ if @singleton__instance__
109 @singleton__mutex__.synchronize {
110 return @singleton__instance__ if @singleton__instance__
111 @singleton__instance__ = new()
113 @singleton__instance__
120 # extending an object with Singleton is a bad idea
121 undef_method :extend_object
123 def append_features(mod)
124 # help out people counting on transitive mixins
125 unless mod.instance_of?(Class)
126 raise TypeError, "Inclusion of the OO-Singleton module in module #{mod}"
133 klass.private_class_method :new, :allocate
134 klass.extend SingletonClassMethods
135 Singleton.__init__(klass)
144 def num_of_instances(klass)
145 "#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
148 # The basic and most important example.
150 class SomeSingletonClass
153 puts "There are #{num_of_instances(SomeSingletonClass)}"
155 a = SomeSingletonClass.instance
156 b = SomeSingletonClass.instance # a and b are same object
157 puts "basic test is #{a == b}"
160 SomeSingletonClass.new
161 rescue NoMethodError => mes
167 puts "\nThreaded example with exception and customized #_instantiate?() hook"; p
168 Thread.abort_on_exception = false
170 class Ups < SomeSingletonClass
173 puts "initialize called by thread ##{Thread.current[:i]}"
179 @enter.push Thread.current[:i]
180 while false.equal?(@singleton__instance__)
181 @singleton__mutex__.unlock
183 @singleton__mutex__.lock
185 @leave.push Thread.current[:i]
186 @singleton__instance__
196 raise "boom - thread ##{Thread.current[:i]} failed to create instance"
211 Thread.current[:i] = i
214 rescue RuntimeError => mes
219 puts "Before there were #{num_of_instances(self)}"
221 puts "Now there is #{num_of_instances(self)}"
222 puts "#{@enter.join '; '} was the order of threads entering the waiting loop"
223 puts "#{@leave.join '; '} was the order of threads leaving the waiting loop"
229 # results in message like
230 # Before there were 0 Ups instance(s)
231 # boom - thread #6 failed to create instance
232 # initialize called by thread #3
233 # Now there is 1 Ups instance(s)
234 # 3; 2; 1; 8; 4; 7; 5 was the order of threads entering the waiting loop
235 # 3; 2; 1; 7; 4; 8; 5 was the order of threads leaving the waiting loop
238 puts "\nLets see if class level cloning really works"
243 raise "boom - thread ##{Thread.current[:i]} failed to create instance"
254 puts "\n\n","Customized marshalling"
257 attr_accessor :persist, :die
259 # this strips the @die information from the instance
260 Marshal.dump(@persist,depth)
265 instance.persist = Marshal.load(str)
270 a.persist = ["persist"]
274 stored_state = Marshal.dump(a)
278 b = Marshal.load(stored_state)
280 p a.persist # => ["persist"]
284 puts "\n\nSingleton with overridden default #inherited() hook"
287 def Up.inherited(sub_klass)
288 puts "#{sub_klass} subclasses #{self}"
296 class Down < Middle; end
298 puts "and basic \"Down test\" is #{Down.instance == Down.instance}\n
305 rescue TypeError => mes
306 puts mes #=> Inclusion of the OO-Singleton module in module AModule
310 'aString'.extend Singleton
311 rescue NoMethodError => mes
312 puts mes #=> undefined method `extend_object' for Singleton:Module