Skip to content
Sign upLog in
← Back to Community

Singleton Design Pattern

Profile icon
DynamicSquidHacker

Hey guys! Haven't posted a tutorial in a while, but I'm back! I guess this will be a new series called "design patterns". Enjoy :)


Imagine you are modelling a President class. You can only have one President, right?

class President {}; President president1; Presdient president2; // uh oh!

Well, that's not good! How can we prevent a class from being instantiated more that once?

Introducing singletons!


Let's code a singleton step by step. Let's start off with a normal class.

class Singleton { private: public: Singleton() {} }; int main() { Singleton a; Singleton b; // uh oh! that's not what we want }

Hmm... the problem is that the constructor is public, so everyone can access it. What if we made a private constructor?

class Singleton { private: Singleton() {} public: }; int main() { Singleton a; // error! Singleton b; // error! }

Well now nothing can access it! How are we even going to create an instance of this? What if we already had an instance in the class? Maybe a private static instance?

class Singleton { private: static Singleton instance; Singleton() {} public: }; // define static variable Singleton Singleton::instance; int main() { }

Okay, well now we have an instance, but how do we access it? Perhaps a public static method?

class Singleton { private: static Singleton instance; Singleton() {} public: static Singleton& GetInstance() // don't forget the reference! { return instance; } }; Singleton Singleton::instance; int main() { Singleton::GetInstance(); // works fine! }

But wait, there's one more issue. You can still create another instance through a copy constructor:

int main() { Singleton a = Singleton::Get(); }

Uh oh! To solve this, C++ has a handy feature called =delete. And if we use that on a copy constructor, it'll essentially prevent that copy constructor from being used.

class Singleton { private: static Singleton instance; Singleton() {} public: Singleton(const Singleton&) = delete; static Singleton& GetInstance() { return instance; } }; Singleton Singleton::instance; int main() { Singleton a = Singleton::Get(); // error! }

And that's it!


Here's some examples:

class Singleton { private: static Singleton instance; Singleton() {} public: int x; void function(); Singleton(const Singleton&) = delete; static Singleton& GetInstance() { return instance; } }; Singleton Singleton::instance; int main() { Singleton::Get().x = 5; Signleton::Get().function(); }

So really, all you need for a singleton are these 3 things:

  1. Private Constructor
  2. Private Static Instance
  3. Public GetInstance() Method

And if you're using C++:

  1. =delete Copy Constructor

Hopefully you learned something new! For more information, check it out my Gist. See you next time!

Voters
Profile icon
HenryHawkins
Profile icon
Highwayman
Profile icon
DynamicSquid
Comments
hotnewtop
Profile icon
Highwayman

YAASS!!

I’ve been having this problem like just this month! Thank you! Lol

Profile icon
DynamicSquid

@Highwayman
oh cool lol :)

Profile icon
Highwayman

😜@DynamicSquid

Profile icon
CodeLongAndPros

Nice, but: Woosh

Profile icon
DynamicSquid

@CodeLongAndPros
I don't get it

Profile icon
CodeLongAndPros

@DynamicSquid
I don't know C++, so it went Woosh over my head.

Profile icon
DynamicSquid

@CodeLongAndPros
ooh... lol