Singleton Design Pattern - sharmasadhna/mylearnings GitHub Wiki

AIM:

create only 1 instance of a class and allow this instance to be used in the entire program cycle

Problems:

against Single Responsibility Principle (SOLID), because solves 2 problems: 1. avoid multiple instances 2. provide global access point to that instance

Implementation: (it's thread safe since we are creating a static object, and returning by reference)

  1. private constructor for class --> Also delete copy const, assign oper for class
  2. public static method of class (getInstance) returns object by reference --> static MySingleton& getInstance(){ static MySingleton instance; return instance; };