Singleton - Design Pattern

| Comments

Problem Statement

We create a class, and we know the classes can be instantiated once or twice or a thousand times, but what if you only want one object of that class.

Solution

Before we dive into solution lets understand what actually is design pattern.

Design Patterns are well-tested solutions to common problems and issues we run into in software development. Think of them as best practices, suggestions for how you might arrange your own classes and objects to accomplish a result.

First approach

To achieve one object solution might be an Abstract Class, but remember an Abstract Class isn’t allowed to be instantiated at all.

Second approach

Second approach might be why don’t we just not create more than one object?
You could do that but it’s not really enforcing anything, you can’t guarantee that behavior.

Singleton

In Singleton Design Pattern, we don’t want to have to write code to instantiate the Singleton, we want to assume that it always exists, there is always one of them, and we just want to be able to ask for it from any other point of the program and get it.

With the Singleton Design Pattern we ensure that a class only has one instance, and we provide just one way to get to it.

Step 1
Mark constructor as Private

What this means is nobody can actually instantiate the class from outside, nobody can just say they want to create a new object. This doesn’t really solve our problem that we need there to be one of them. We just can’t create it from the outside.

Singleton
public class MySingleton {

        // private constructor - no object can instantiate
        private MySingleton() { }
        
        ...
        ...
}

Step 2
Create a static variable in this class that holds a placeholder to a singleton object

This just means a variable that can hold a singleton object, and there is nothing in it right now.

Singleton
public class MySingleton {
        // placeholder for singleton object
        private static MySingleton _singleObj = null;

        // private constructor - no object can instantiate
        private MySingleton() { }
        
        ...
}

Step 3
Create a static method say getInstance().

Static methods can only access static data members. In this case, we don’t have to have an instance of this class yet. The first thing it asks is do I exist? Is there anything in that _singleObj variable, if it’s null then instantiate a new MySingleton object.

Singleton
public class MySingleton {
        // placeholder for singleton object
        private static MySingleton _singleObj = null;

        // private constructor - no object can instantiate
        private MySingleton() { }
        
        public static MySingleton getInstance() {
            // Do I exists ?
            if (_singleObj == null) {
                    // Instantiate object
                    _singleObj = new MySingleton();
            }
            return _singleObj;            
        }
        
        // Other member functions
        ...
}

We’re allowed to do instantiate the object in getInstance() from inside this class, because we marked that constructor as private, which means this class is allowed to call it but nobody else is.

We are actually using a technique here called Lazy Instantiation, which means that until someone asks for this object it doesn’t exist. But when they ask for it, we’ll look for it. If it doesn’t exist, we create it, we store a reference to it so that when the next person asks it’s already there.

Finally to invoke Singleton

Singleton
MySingleton single = MySingleton.getInstance();

single.otherMemberFunctions();



design pattern

« OOPS - Object Oriented Curious Case Of Recursion »

Comments