Hi
Could someone tell me what's the difference between these two singleton
implementations:
First implementation:
class Singleton
{
public:
static Singleton& instance() { return _instance; }
//... other members
private:
Singleton();
static Singleton _instance;
//... other members
};
Second implementation:
class Singleton
{
public:
static Singleton* instance() { return _instance; }
static void Initialize() { _instance = new Singleton; }
//... other members
private:
Singleton();
static Singleton* _instance;
//... other members
};
I know that in the first implementation, _instance will be put in the
static memory, and it will always be present, whereas in the second
implementation, memory will only be allocated on the heap after
Singleton::Initialize() is called. But aside from that, is there any
practical difference between the two, like on what circumstances is the
first implementation preferred over the second (or vice versa)?
Thanks! 10 5281
For one thing, the pointer implementation has no clean up code - so any
resources the singleton uses may not be returned.
regards,
Aiden
<fe****************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com... Hi
Could someone tell me what's the difference between these two singleton implementations:
First implementation:
class Singleton { public: static Singleton& instance() { return _instance; } //... other members private: Singleton(); static Singleton _instance; //... other members };
Second implementation:
class Singleton { public: static Singleton* instance() { return _instance; } static void Initialize() { _instance = new Singleton; } //... other members private: Singleton(); static Singleton* _instance; //... other members };
I know that in the first implementation, _instance will be put in the static memory, and it will always be present, whereas in the second implementation, memory will only be allocated on the heap after Singleton::Initialize() is called. But aside from that, is there any practical difference between the two, like on what circumstances is the first implementation preferred over the second (or vice versa)?
Thanks!
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
---------------------------------------------------------- http://www.usenet.com
Yes, but let's say I add another method, say CleanUp(), in the second
implementation that deletes _instance, and users are required to call
Initialize and CleanUp before and after using the class, respectively.
Are the first and second now functionally identical? Or is there a
situation where one implementation is better than the other? (e.g. in a
multi-threaded application, etc.)?
Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.
The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();
return pInst;
}
That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.
hope that helps,
regards,
Aiden
<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com... Yes, but let's say I add another method, say CleanUp(), in the second implementation that deletes _instance, and users are required to call Initialize and CleanUp before and after using the class, respectively. Are the first and second now functionally identical? Or is there a situation where one implementation is better than the other? (e.g. in a multi-threaded application, etc.)?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
---------------------------------------------------------- http://www.usenet.com
Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.
The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();
return pInst;
}
That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.
hope that helps,
regards,
Aiden
<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com... Yes, but let's say I add another method, say CleanUp(), in the second implementation that deletes _instance, and users are required to call Initialize and CleanUp before and after using the class, respectively. Are the first and second now functionally identical? Or is there a situation where one implementation is better than the other? (e.g. in a multi-threaded application, etc.)?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
---------------------------------------------------------- http://www.usenet.com
Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.
The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();
return pInst;
}
That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.
hope that helps,
regards,
Aiden
<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com... Yes, but let's say I add another method, say CleanUp(), in the second implementation that deletes _instance, and users are required to call Initialize and CleanUp before and after using the class, respectively. Are the first and second now functionally identical? Or is there a situation where one implementation is better than the other? (e.g. in a multi-threaded application, etc.)?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
---------------------------------------------------------- http://www.usenet.com
Apart from the explicit vs implicit memory management there is no real
difference - as far as multi - threading is concerned, you need to employ
some synchronisation primitives to protect simulataneous access to your
singleton - the same situation exists in both cases.
The 'popular' issue with threading, is a third possibile implementation
whereby:
MySingleton * instance()
{
static MySingleton * pInst = new MySingleton();
return pInst;
}
That changes your synchronisation requirements - but how is very dependent
on your particular design/usage.
hope that helps,
regards,
Aiden
<fe****************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com... Yes, but let's say I add another method, say CleanUp(), in the second implementation that deletes _instance, and users are required to call Initialize and CleanUp before and after using the class, respectively. Are the first and second now functionally identical? Or is there a situation where one implementation is better than the other? (e.g. in a multi-threaded application, etc.)?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
---------------------------------------------------------- http://www.usenet.com
adbarnet wrote: Apart from the explicit vs implicit memory management there is no real difference - as far as multi - threading is concerned, you need to employ some synchronisation primitives to protect simulataneous access to your singleton - the same situation exists in both cases.
The 'popular' issue with threading, is a third possibile implementation whereby: MySingleton * instance() { static MySingleton * pInst = new MySingleton();
return pInst; }
....
Doing is this way, with a static 'inside' a function guarantees that
MySingleton will be initialised even if you try and access it before
main() is called. e.g from another singleton, or initialisation.
Ok, just to be complete, lets add the fourth implementation
class Singelton
{
Singelton();
public:
static Singelton& Instance() { static Singelton s; return s; }
};
cheers fe****************@gmail.com wrote: Yes, but let's say I add another method, say CleanUp(), in the second implementation that deletes _instance, and users are required to call Initialize and CleanUp before and after using the class, respectively. Are the first and second now functionally identical? Or is there a situation where one implementation is better than the other? (e.g. in a multi-threaded application, etc.)?
Read up on the "static initialization order fiasco" in this newsgroup's FAQ. fe****************@gmail.com wrote: Hi
Could someone tell me what's the difference between these two
singleton implementations:
You don't control initialization in the first case. In the second case
you do.
I usually do something like this:
class Singleton
{
Singleton *instance;
Singleton() {}
public:
void function1() { blah; }
static void function1 { if (instance == NULL) instance = new
Singleton();
instance->function1(); }
};
in Singleton.cpp:
Singleton *Singleton::instance = NULL;
I don't know off hand if C++ allows static and non-static functions to
have same signature...you may have naming issues w/ regard to above.
It is the basic structure that is important. You will probably want a
const accessor also. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Richard A. DeVenezia |
last post by:
foo() generates elements with event handlers that invoke foo function
properties.
Is this an abhorrent or misthought pattern ?
It allows just the one occurence of identifier /foo/ to be changed...
|
by: Simon |
last post by:
Hi.
I don't have a problem per se, I was just wondering if anyone can
offer some opinions about the best way to go about creating and
disposing of a Singleton class.
I have a class (handling...
|
by: Eric |
last post by:
Perhaps this question has been posed before (I'd be surprised if it
hasn't) but I just gotta know...
Is it possible to combine the Singleton and Factory Method design
patterns in the same class?...
|
by: Alicia Roberts |
last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic...
|
by: Sharon |
last post by:
I wish to build a framework for our developers that will include a singleton
pattern.
But it can not be a base class because it has a private constructor and
therefore can be inherit.
I thought...
|
by: Gaensh |
last post by:
HI,
I have a singleton pattern to acess my database the following is
the sample code use to implement singleton pattern
public class DataAccessHelper
{
private static DataAccessHelper instance;...
|
by: Kevin Newman |
last post by:
I have been playing around with a couple of ways to add inheritance to a
JavaScript singleton pattern. As far as I'm aware, using an anonymous
constructor to create a singleton does not allow any...
|
by: Diffident |
last post by:
Hello All,
I am designing a class based on singleton pattern. Inside this class I have
multiple instance methods. My question is since there will be only one
instance of this class at any...
|
by: weaknessforcats |
last post by:
Design Pattern: The Singleton
Overview
Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
|
by: Bob Johnson |
last post by:
Just wondering the extent to which some of you are implementing classes as
Singletons. I'm working on a brand new project and, early on, identified
some obvious candidates. By "obvoius candidates"...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |