Design Patterns in Software Engineering : Singleton Design Pattern in C++
@google_photos

Design Patterns in Software Engineering : Singleton Design Pattern in C++

Design patterns are some sort of coding styles that can ease your development process in a large project. It's goal is to get you an easy, extensible and error free programming experience. C++ is a great language to learn the basic design patterns. There are mainly three main design pattern principals, see the following structure :

  1. Creational Patterns: i) Abstract Factory ii) Builder iii) Factory Method iv) Prototype v) Singleton
  2. Structural Patterns
  3. Behavioral Patterns

here I will mostly discuss about the creational patterns and in our first post we will discuss about singleton pattern. I am a strict believer of hands on coding so we will see the code example first followed by the discussion.


#include <iostream>
#include <thread>
#include <string>
#include <memory>

using namespace std;

class SingleTon {
public:
    SingleTon(SingleTon& st) = delete;
    SingleTon operator = (const SingleTon& st) = delete;

    static SingleTon* GetInstance(string n);
    void PrintName() const {
        cout << _name << endl;
    }
private:
    SingleTon(string name):_name{name} {}

    static SingleTon* _SingleTon;
    string _name;
};

SingleTon* SingleTon::_SingleTon = nullptr;

SingleTon* SingleTon::GetInstance(string n) {
    if (_SingleTon == NULL) {
        _SingleTon = new SingleTon(n);
    }
    return _SingleTon;
}

int main()
{
    SingleTon* st = SingleTon::GetInstance("singleton one");
    SingleTon* st1 = SingleTon::GetInstance("singleton two");

    st->PrintName();
    st1->PrintName();

    return 0;
}        

now if you see my example, I have taken a class called SingleTon and this class bears the example of the named pattern. Singleton means the class won't have more than one instance through out the life span of the program, meaning we can only create one object of this class. settings of a specific game is the best example of singleton because you will see in a single profile we can only change the settings and it will hold the last change.


Article content
output of the previous code

now if we check the output we can clearly see for both pointers even if we assign different values in the constructors the value printed by them is same and the ctor called first is the value they store. This is because if you see our GetInstance() function it's clearly written that if the static pointer is not NULL then we won't change the value of this pointer otherwise we will assign a new SingleTon() object pointer to this one.

Some features / characteristics of Singleton :

  • the constructors and assignment operator needs to be inside the private access modifier, so that no one can create an object in stack.
  • the class should have a static pointer of itself, static because it's not changeable throughout.
  • this should have a public method which will return a static pointer to this class, conventionally static SingleTon* GetInstance().


hopefully I was able to clarify about singleton classes and the design pattern associated with it. Follow this series for more such design patterns in C++. This being my first article will definitely have some issues, please comment down your thoughts so I can rectify my mistakes and provide more meaningful coding content.

10 Taypes tribal pattern design images top quality premium product list Click here.https://jaeger11.gumroad.com/l/hiaklp

Like
Reply

Just a quick correction: If you declare the copy assignment operator and copy constructor as private in your class, there's no need to explicitly mark them as deleted. Moreover, to ensure thread safety in a multithreading environment, it's essential to incorporate proper synchronization mechanisms. I've discussed the implementation of the Singleton pattern with thread safety in my recent LinkedIn post: https://www.garudax.id/posts/satyam-kumar-1707141_singletonpattern-multithreading-designpatterns-activity-7139471952150151169--lGl?utm_source=share&utm_medium=member_desktop

ATA kon platform likhechen ?

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore content categories