Skip to content

Instantly share code, notes, and snippets.

@czs0x55aa
Created October 2, 2017 12:43
Show Gist options
  • Save czs0x55aa/5eceb06c6be5ae59136efa134e6c8389 to your computer and use it in GitHub Desktop.
Save czs0x55aa/5eceb06c6be5ae59136efa134e6c8389 to your computer and use it in GitHub Desktop.

Python

def singleton(cls):
    instance = cls()
    instance.__call__ = lambda: instance
    return instance

@singleton
class Highlander:
    x = 100

Java

public class Singleton {  
    private static class SingletonHolder {  
        private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
    public static final Singleton getInstance() {  
        return SingletonHolder.INSTANCE; 
    }  
}

C++

class Singleton {
public:
    static Singleton& Instance() {
        static Singleton theSingleton;
        return theSingleton;
    }

private:
    Singleton();                            // ctor hidden
    Singleton(Singleton const&);            // copy ctor hidden
    Singleton& operator=(Singleton const&); // assign op. hidden
    ~Singleton();                           // dtor hidden
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment