![]() |
Search for Robust Singleton Design Pattern
BackgroundNormally most of software Engineer uses singleton design pattern in daily coding life. It's very simple , vast uses but still i saw in many softwares which is not handled proper in C++ based products/projects. One thing more you can'nt design singleton as perfectly behaved singleton. But here approch and solution for how to make perfact singleton class in C++. Why Singleton: To maintain object as single instance which is used by more than one user. Details & Step by Step ImprovementStep 1: In first sight, singleton can be create as Code:
class SingletonStep 2: Adding Copy Constructor in private section Code:
class SingletonNow Again Some people (Now i hope 95%) will say it's not complete design, we should take of memory management. Okay Now we will go for that. Step 3: Adding memory management code Code:
class SingletonOkay It can be done by little bit changes. Step 4: Adding memory management code ( Continued ) Code:
class SingletonNow I hope 90% people will say again it has problem, suppose lot of users using this application then ... Suppose X guy uses this object and release it and other guy Y still wants to use this object then problem.....Okay lets see in next step. Step 5: Introducing reference counts (In our software a class is available for it) Code:
class SingletonNow hope people will say it's perfect solution. But still i hope 80% people will say it has problem ...Suppose One user calling DestroyMe() function more than one times, lets say five times then no probs for a while( on that time refCount will be -4 ) but problem will be raised when next user is calling createSingleton() (refCount will be -3) and calking DestroyMe() in which destructor itself will not be called. A liitle bit changes can be done. Code:
Singleton :: DestroyMe()One more have putted destructor in public better if we will put in private because more constructor which is not in private can be putted in public section. To avoid this situation better if you put destructor in private section. Coplete design of singleton is as: Code:
class SingletonReferences1. GoF http://www.vincehuston.org/dp/ 2. http://hitechi.19.forumer.com/viewtopic.php?t=41 3. Idea for Style of writing this article is taken from code guru. Note: Code written in this article is morally as designed purpose so dont expect it's runnable code. You may get compilation error. |
Re: Search for Robust Singleton Design Pattern
Sorry for telling you that, but your "robust" singleton design is closer to disastrous than perfect.
First of all, the code itself doesn't reflect your description. Your intentions are good, but your code doesn't reflect your intentions. I know the Singleton design is supposing to have only one instance per "session", so I don't see the reason to dynamically allocate the object instead of pushing it onto the stack, and being destroyed automatically by the runtime library. This way no "memory management" will be needed for the Singleton design. Anyway I will skip that.. Step 5 - Introducing reference counters is interesting The reference counting ... doesn't count. Code:
Singleton* Singleton :: CreateSingleton()Code:
Singleton :: DestroyMe()Basically, NEVER. Even if you will fix the IF condition clause, the reference is decremented only when the pointer is deleted and set to null. So only once per allocation. In this scenario, your counter isn't a ref-counter, is more a redundant variable. One more aspect that the "perfect" design is missing, is the thread safety of the singleton model. You force the user to use the singleton instance from one thread only. If the user uses this model in a multi-threaded application it may collapse under allocation and deallocation routines which are not designed to support that. Cheers |
Re: Search for Robust Singleton Design Pattern
Yes!!! You are right...
Correct code will be as: Step 5: Introducing reference counts (In our software a class is available for it) Code:
class SingletonCode:
Singleton :: DestroyMe() |
Re: Search for Robust Singleton Design Pattern
Even if we leave thread safty for a while then there are still few problems here.
1. First you don't have to write empty body for copy ctor you can do the same thing with this Code:
class SingletonQuote:
3. Third the static member variable of Singlton has different name in class and in body, so this code wont even compile Quote:
Quote:
Quote:
Code:
void Singleton :: DestroyMe()Code:
if( NULL != InstSingleton_mp && 0 == refCount)Code:
~Singleton() {};7. Seventh and probably the most important one is what will be the situation when someone write a code something like this Code:
Singleton* pInstance1 = Singleton::CreateSingleton();Please at least compile the code before posting, so at least reader wont get at least compilation errors. |
Re: Search for Robust Singleton Design Pattern
|
Re: Search for Robust Singleton Design Pattern
Quote:
On your first Point mentioning that .... "I know the Singleton design is supposing to have only one instance per "session", so I don't see the reason to dynamically allocate the object instead of pushing it onto the stack, and being destroyed automatically by the runtime library. This way no "memory management" will be needed for the Singleton design. Anyway I will skip that.". As this class is singleton it will have only one instance...and How will you make sure that this having only one instance that is why we dynamically allocate memory for it on heap. so that the instance is presistent for all the functions or stacks created on your program. And can you tell me where and how you can design a singleton class without using dynamic allcation. |
Re: Search for Robust Singleton Design Pattern
Quote:
Hi, You forgaot to add operator overloading for =() to add the counts and subtract the counts when they are assigned to NULL |
Re: Search for Robust Singleton Design Pattern
Quote:
Without going into advantages and disadvantages here is the simplest implementation of Singleton without using Heap Code:
Singleton* Singleton::CreateObject() |
Re: Search for Robust Singleton Design Pattern
Quote:
Good that u tried to do one without dynamic allocation. But one thing we should be carefull about is Static objects get created at start of program, so the objects gets created before main() which calls its constructor before. How we implement inside constructor also matters. |
Re: Search for Robust Singleton Design Pattern
Quote:
In fact you can always make a program to test it. Here is the simple test program to verify this Code:
#include <iostream> |
| All times are GMT +5.5. The time now is 10:39. |