Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Singleton implemention using pointer and using static object

EDIT: Sorry my question was not clear, why do books/articles prefer implementation#1 over implementation#2?

What is the actual advantage of using pointer in implementation of Singleton class vs using a static object? Why do most books prefer this

class Singleton {   private:      static Singleton *p_inst;     Singleton();    public:      static Singleton * instance()     {       if (!p_inst)       {         p_inst = new Singleton();       }        return p_inst;     } }; 

over this

class Singleton {   public:     static Singleton& Instance()     {         static Singleton inst;         return inst;     }    protected:     Singleton(); // Prevent construction     Singleton(const Singleton&); // Prevent construction by copying     Singleton& operator=(const Singleton&); // Prevent assignment     ~Singleton(); // Prevent unwanted destruction }; 
like image 940
Anon Avatar asked Oct 24 '12 10:10

Anon


People also ask

What is the difference between singleton and static method and when to use?

While a static class allows only static methods and and you cannot pass static class as parameter. A Singleton can implement interfaces, inherit from other classes and allow inheritance. While a static class cannot inherit their instance members. So Singleton is more flexible than static classes and can maintain state.

What is the difference between singleton design pattern and static class?

A singleton allows access to a single created instance - that instance (or rather, a reference to that instance) can be passed as a parameter to other methods, and treated as a normal object. A static class allows only static methods.

What is singleton design pattern why it is used for Why not static class?

Singleton is a design pattern that makes sure that your application creates only one instance of the class anytime. It is highly efficient and very graceful. Singletons have a static property that you must access to get the object reference.

What is difference between singleton and static class in Java?

The singleton, like any other instance of a class, lives on the heap. To its advantage, a huge singleton object can be lazily loaded whenever required by the application. On the other hand, a static class encompasses static methods and statically bound variables at compile time and is allocated on the stack.


1 Answers

why do books/articles prefer implementation#1 over implementation#2?

Because most articles describing the Singleton anti-pattern don't fully understand all the hidden dangers when trying to implement it safely in C++. It's surprisingly difficult to get it right.

What is the actual advantage of using pointer in implementation of Singleton class vs using a static object?

Using a pointer, with new but no delete, ensures that the object will never be destroyed, so there is no danger of accessing it after its lifetime has ended. Combined with the "lazy" creation, the first time that it's accessed, this guarantees that all accesses are to a valid object. The disadvantages are that creation is not thread-safe, and that the object and any resources it acquires are not released at the end of the program.

Using a local static object, creation is thread-safe on any compiler that supports the C++11 threading model; also, the object will be destroyed at the end of the program. However, it is possible to access the object after its destruction (e.g. from the destructor of another static object), which could lead to nasty bugs.

The best option is to avoid static data, and globally-accessible data, as much as possible. In particular, never use the Singleton anti-pattern; it combines global, static data with weird instantiation restrictions that make testing unnecessarily difficult.

like image 137
Mike Seymour Avatar answered Sep 22 '22 02:09

Mike Seymour