singleton-class

class << self idiom in Ruby

I suppose my question is exactly what the subject depicts, what does: class << self do in Ruby? ...

Do I still have to implement a singleton class by hand in .net, even when using .Net4.0?

Once the singleton pattern is understood, writing subsequent singleton classes in C# is a brainless exercise. I would hope that the framework would help you by providing an interface or a base class to do that. Here is how I envision it: public sealed class Schablone : ISingleton<Schablone> { // Stuff forced by the interface goes he...

Given an instance of a Ruby object, how do I get its metaclass?

Normally, I might get the metaclass for a particular instance of a Ruby object with something like this: class C def metaclass class << self; self; end end end # This is this instance's metaclass. C.new.metaclass => #<Class:#<C:0x01234567>> # Successive invocations will have different metaclasses, # since they're different ins...

Python singleton pattern

Hi, someone can tell me why this is incorrect as a singleton pattern: class preSingleton(object): def __call__(self): return self singleton = preSingleton() # singleton is actually the singleton a = singleton() b = singleton() print a==b a.var_in_a = 100 b.var_in_b = 'hello' print a.var_in_b print b.var_in_a Edit:...

General question about Ruby singleton class

module MyModule def my_method; 'hello'; end end class MyClass class << self include MyModule end end MyClass.my_method # => "hello I'm unsure why "include MyModule" needs to be in the singleton class in order to be called using just MyClass. Why can't I go: X = MyClass.new X.my_method ...

Objective C: Memory Leak of Dictionary used in singleton..

I am using a singleton class to share data between views in my iphone app. My singleton class contains a dictionary which I allocate in my -init method: - (id)init { if ( self = [super init] ) { self.dataList = [[NSMutableDictionary alloc]init]; } return self; } I release it in my dealloc metho...

How to deal with Singleton along with Serialization

Consider I have a Singleton class defined as follows. public class MySingleton implements Serializable{ private static MySingleton myInstance; private MySingleton(){ } static{ myInstance =new MySingleton(); } public static MySingleton getInstance(){ return MySingleton.myInstance; } } The above definition according to...