Menu

Fashion Trendy
  • Drop Down

    • Abstract
    • Model
    • Techo
    • Options
  • Photography Pictures Product

    Drop Menu

    • Crystal
    • Digital
    • Graphs
    • Settings
  • Menu

    Stack Now | C, C++, Linux, Design Patterns etc

    • Home
    • Digital Art
      • Pics
        • SEO 1
        • SEO 2
      • CSS
        • CSS 1
        • CSS 2
        • CSS 3
        • CSS 4
        • CSS 5
      • Jquery
        • Jquery 1
        • Jquery 2
    • Fashion
      • Product 1
        • Sub Item
        • Sub Item
      • Product 2
        • Sub Item
        • Sub Item
    • Photography
    • Design
    Go
    Home » C++ » Design Patterns » Singleton Design Pattern - My View

    Singleton Design Pattern - My View

    Singleton Design Pattern my view.jpg
    I had used singleton design pattern in one of my projects. And in this post I will express my views about it.

    Singleton design pattern is a creational design pattern. This design pattern ensures that only one object is created in the lifetime of the program.

    The object is created at the time of first access and the same instance is used thereafter in the entire program. This can be achieved by making constructor, destructor, copy constructor and assignment operator all private in the class.

    Check out the basic example of a singleton class below:

    class SingletonExample
    {
    public:
      static SingletonExample *GetInstance()
      {
         return (obj ? obj : new SingletonExample());
      }

      void FreeSingleton()
      {
         free(obj);
         obj = NULL;
      }
    private:
      static SingletonExample *obj;
      SingletonExample();
     ~SingletonExample();
      SingletonExample (const SingletonExample&);
      SingletonExample& operator=(const SingletonExample&);
    };


    The above singleton class solves most of the issues but what if there are two threads and both threads are called at same time and both try to create the instance?
    To prevent creation of another instance, you need to use mutex in this case. By locking and unlocking the mutex in the critical section of GetInstance function, you can fix the issue:

    In a project, if you need to create a Logger class which logs the user data to a file, Singleton is the best design pattern to use.

    Please let us know in comments if you have any queries about the Singleton Design Pattern.
    yash
    Add Comment
    C++ Design Patterns
    Thursday, March 13, 2014

    facebook

    twitter

    google+

    fb share

    About yash

    Related Posts
    < Previous Post Next Post >

    Menu
    • Home
    • C
    • C++
    • Linux
    • Design Patterns
    • Programming Tips

    Popular Posts

    • 8 C++ String Functions You Must Know!
      In C++, String is one of the most used class to represent a sequence of characters. String class is an instantiation of the basic_string c...
    • How to Print Time in Particular Format in C/C++?
      You can print time in any format using strftime function in C/C++. The function declaration is as below: size_t strftime (char* buf, i...
    • 2-Minute Guide for Patching and Generating a Patch File
      Many times in the project, you need to generate a patch file or patch the file to your project. This 2 minute guide will help you how to g...
    • How to Write Better Programs in C/C++?
      I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with w...
    • Singleton Design Pattern - My View
      I had used singleton design pattern in one of my projects. And in this post I will express my views about it. Singleton design pattern i...

    Social Share

    Weekly Posts

    • 8 C++ String Functions You Must Know!
      In C++, String is one of the most used class to represent a sequence of characters. String class is an instantiation of the basic_string c...
    • How to Print Time in Particular Format in C/C++?
      You can print time in any format using strftime function in C/C++. The function declaration is as below: size_t strftime (char* buf, i...
    • 2-Minute Guide for Patching and Generating a Patch File
      Many times in the project, you need to generate a patch file or patch the file to your project. This 2 minute guide will help you how to g...
    • How to Write Better Programs in C/C++?
      I have been coding for the past 7 years in a MNC and had experienced a lot of programming rules. Here I am sharing some of the tips with w...
    • Singleton Design Pattern - My View
      I had used singleton design pattern in one of my projects. And in this post I will express my views about it. Singleton design pattern i...

    Like us On Facebook

    Labels

    • C
    • C++
    • Design Patterns
    • linux
    • Programming Tips

    Copyright Stack Now | C, C++, Linux, Design Patterns etc 2014 . Template Created by