Hello people my name is Ridit Mukhopadhyay and today i will be discussing the Singleton pattern.In Singleton pattern there is going to be only one object or instance. E.g Restaurant. There is only going to be one restaurant system.The basic idea of singleton is that it should have a private constructor which cannot be accessed outside of the class. Let me explain what is happening.The first thing that we do is make a static member variable called instance. Next we change the constructor to private. After that we make a method called getInstance who’s return type will be Singleton*.

Let me explain why we are making the method a static method. It’s simple. If the variable is in the class scope the method which will be accessing it will also have to be static.

Let's get back to the code.

The thing i forgot to mention is that we will make an id variable which functions like java’s hashcode function of an object. We have to implement

The following lines

 #include <boost/uuid/uuid.hpp>

#include <boost/uuid/uuid_generators.hpp>

#include <boost/uuid/uuid_io.hpp>

For the id variable:

boost::uuids::uuid id;

This should be done in the private Singleton Constructor:

id = boost::uuids::random_generator()();

This will be using the boost's own function since this isn't something c++ has implemented itself.

Next we are going to talk about the getId function. The getId function is a function which obviously gets the id it is very simple:

boost::uuids::uuid getId(){

        return id;

}

Now the most important thing is initialising instances to null outside of the class.

Note: the reason is that static code of the class loads into the memory before the Main is loaded since it is static that is why we have to initialise it.

In the main, we are going to be making two Singleton objects, let's call them object 1 and object 2.We are going to create them using the getInstance function:

Singleton* object1 = Singleton::getInstance();

Singleton* object2 = Singleton::getInstance();

After that we are going to check if the ids are matching or not.We are going to show a message that it is a singleton:

if(object1->getId() == object2->getId()){

        cout<<"This is a Singleton"<<endl;

}

The full source code:

Singleton done only in the header file:

/*

* Singleton.h

*

* Created on: 02-Feb-2023

* Author: ridit

*/

#ifndef SINGLETON_H_

#define SINGLETON_H_

#include <iostream>

#include <boost/uuid/uuid.hpp>

#include <boost/uuid/uuid_generators.hpp>

#include <boost/uuid/uuid_io.hpp>

using namespace std;

class Singleton {

private:

        static Singleton* instance;

        boost::uuids::uuid id;

        Singleton(){

                id = boost::uuids::random_generator()();

        }

public:

        static Singleton* getInstance(){

                if(instance == NULL){

                        instance = new Singleton();

                }

                return instance;

        }

        boost::uuids::uuid getId(){

                return id;

        }

        virtual ~Singleton(){

        }

};

Singleton* Singleton::instance = NULL;

#endif /* SINGLETON_H_ */

Main:

//============================================================================

// Name : Singleton2.cpp

// Author : Ridit

// Version :

// Copyright : @Ridit...

// Description : Hello World in C++, Ansi-style

//============================================================================

#include <iostream>

#include "Singleton.h"

using namespace std;

int main() {

        Singleton* object1 = Singleton::getInstance();

        Singleton* object2 = Singleton::getInstance();

        cout<<object1->getId()<<endl;

        cout<<object2->getId()<<endl;

        if(object1->getId() == object2->getId()){

                cout<<"This is a Singleton"<<endl;

        }

        return 0;

}