Singleton Design

Before you start

What to expect

  • What is a Singleton
  • Why do we need it
  • How to make one
  • Refactoring

Singleton

What is a Singleton?

Why Singleton?

Lets make one

First we Make a Class named ConnectionManager. And give it a constructor.

1:    public class ConnectionManager  
2: {
3: public ConnectionManager()
4: {
5: //Some initialization
6: }
7: }

Here’s the 1st problem. Our Constructor is public so you can always Create an object with

new ConnectionManager();

So we would like to prevent anyone from calling the constructor from outside. We need to make it private.

1:    public class ConnectionManager  
2: {
3: private ConnectionManager()
4: {
5: //Some initialization
6: }
7: }

Now nobody can access the constructor from outside of the class. Now we are found with a new problem. So how do we get an instance of the object? We do want ONE instance. But the only way to create an instance we must need to call the constructor. WHICH by the way we just made private. So there is no way we can get an instance. For this we will take the help of static reference and keep one reference inside of the class.

1:    public class ConnectionManager  
2: {
3: public static ConnectionManager Instance = new ConnectionManager();
4: private ConnectionManager()
5: {
6: //Some initialization
7: }
8: }

Now we can get the instance of the class without accessing the constructor directly. Like this

ConnectionManager.Instance;

This is still far from what we would like to have. Because every time we GET the instance it just creates a new instance and gives us that. We don’t want that. What we want is , If the instance does not have a reference of the object then we would want to get a new instance else we would like to get the existing instance. So What we would like to do is write a function that does exactly that. We need to add logic. And also lets make that Instance variable private so nobody can access it directly either.

1:   public class ConnectionManager  
2: {
3: private static ConnectionManager Instance;
4: private ConnectionManager()
5: {
6: //Some initialization
7: }
8: public static ConnectionManager GetInstance()
9: {
10: if (Instance==null)
11: {
12: Instance=new ConnectionManager();
13: }
14: return Instance;
15: }
16: }

Now if we call Connectionmanager.GetInstance();

We will get exactly what we wanted. Yeah that’s all there is to it. You got yourself a singleton class. Whenever you call GetInstance it will give you the only existing object reference on the application.

BUT WAIT

1:   public static ConnectionManager GetInstance()  
2: {
3: return Instance ?? (Instance = new ConnectionManager());
4: }

I would leave this ?? operator for readers to do research. But I’ll explain the code. Now you see, We always want the function to return An instance. so we are always returning the Instance variable. What we need to check is if the instance is null, We need to assign a new instance and return that. That’s what I did in one line.

It can be even better

1:    public class ConnectionManager  
2: {
3: private static ConnectionManager _singleton;
4: public static ConnectionManager Instance => _singleton ?? (_singleton = new ConnectionManager());
5: private ConnectionManager()
6: {
7: //Some initialization
8: }
9: }

Now not only the code is more clean but also you can use the Instance like a variable instead of a function. Like this Connectionmanager.Instance;

But remember that this property is a C# feature. if you are writing a JAVA code you would need a function to get the instance.

Every public function inside of the singleton class can be accessed via that instance. Now you can’t even create two Database connecting by mistake and save yourself from destroying your query system.

--

--

Author — “How to Make A Game” / Apress, Springer Nature | Head Of Unity Department at Brain Station 23 Limited | Co-Founder of Capawcino Cat Cafe

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tanim-ul Haque Khan

Author — “How to Make A Game” / Apress, Springer Nature | Head Of Unity Department at Brain Station 23 Limited | Co-Founder of Capawcino Cat Cafe