Singleton design pattern basic concept and implementations

Singleton is a creation design pattern according to Gang Of Four book ( it’ s a book made  by 4 persons  with a big knowledge of programming also called  “Design Pattern: Elements of Reusable Object-Oriented Software” )

basically a singleton is a class to instantiate one time  in  execution life. a singleton is used when we manage a class throw one and only one instance: for example to avoid waste of memory resources, or having global point of access to this one instance from all other classes in the same application . It’s very helpful for logger or configuration resources loader.

To create singleton we need to answer 2 questions:

-How to get access on the alone singleton instance from every where?

-How  to instantiate the singleton class only one time during execution life (in other words  how to  make impossible to instantiate more than one instance)?

So it does not matter if we develop in Java, C++, C#,  or Objectif C , the important thing is to ask throw the programming language the previous  2 questions

I have made 4  singleton implementations all of them are compliance with sonar quality check  and are well commented:


Leave a Reply

Your email address will not be published. Required fields are marked *