473,800 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternative was to implement a Singleton Design Pattern

Hello,

currently I am reading the book "Effective C++ Third Edition" from Scott
Meyers. While Reading Item 4 (Make sure that objects are initialized
before they're used) I got an idea how to improve the way to implement
the Singlton Design Pattern.

This is the way I used to do it:

Singleton.h
----------

class Singleton {
public:
static Singleton* getInstance() {
if (!m_instance) {
m_instance = new Singleton();
}

return m_instance;
}

private:
Singleton() { }
~Singleton() { }
static Singleton* m_instance;
}

Singleton.cpp
-------------

Singleton* Singleton::m_in stance = 0;


This is the way I am planning to do it in future:

Singleton.h
-----------

class Singleton {
public:
static Singleton* getInstance() {
static Singleton instance;
return &instance;
}

private:
Singleton() { }
~Singleton() { }
};

(Source is only showing the concept. I didn't compile it.)
I see the following pros:
- No potentional memory leak when process terminates.
- Less source code required.
- Not necessarely a .cpp file required for the static member.

Are there any cons I am missing?

Thanks for your thoughts,
Daniel Kay
Sep 3 '06 #1
2 2630
Daniel Kay wrote:
This is the way I am planning to do it in future:

Singleton.h
-----------

class Singleton {
public:
static Singleton* getInstance() {
static Singleton instance;
return &instance;
}

private:
Singleton() { }
~Singleton() { }
};

(Source is only showing the concept. I didn't compile it.)
I see the following pros:
- No potentional memory leak when process terminates.
- Less source code required.
- Not necessarely a .cpp file required for the static member.

Are there any cons I am missing?
See Alexandrescu's "Modern C++ Design". It discusses singletons in more
detail than you'd ever want to know, and in particular, he discusses
this idiom (he calls it a "Meyers Singleton", since Meyers is the
earliest cite he found it in, I guess).
Sep 3 '06 #2
Daniel Kay wrote:
I see the following pros:
- No potentional memory leak when process terminates.
- Less source code required.
- Not necessarely a .cpp file required for the static member.
singleton would prefferably be implemented as a template
so this is not realy a benefit
>
Are there any cons I am missing?
I don't think there is a single implementation that satisfies
all possible requirements.

few cons might arrise

- No control over singleton's liffetime, can't delete it
see http://www.research.ibm.com/designpa...s/ph-jun96.txt
<no link intentional>
- Singleton must have default constructor
- if Singleton's constructor throws you might not be able to recover
- I can do
delete Singleton::getI nstance()
invoking undefined behaviour, bether return reference
instead of a pointer

otherwise it's fine :)

Sep 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3222
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
4
2424
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be somewhat unusual places where multi-instance singleton is more useful than plain singleton, it seems to me that the former leads to less coding, so unless I can somehow package the singleton pattern in a superclass (so I don't have to code it...
7
12480
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
3
2501
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 what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
21
2471
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 maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
14
3037
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is can a singleton class have a number of paramterised constructors enabling me to pass in parameters or not? I am trying to use the following to send in a parmeter to a constructor, but getting an error with it. I have a feeling that I am not...
9
2963
by: FluffyCat | last post by:
To add to my growing library of Design Patterns in PHP 5 I have written what I think is a good example of the Singleton Pattern. http://www.fluffycat.com/PHP-Design-Patterns/Singleton/ In the classic singleton pattern an object will distribute one and only one instance of itself. This can be useful for the sharing of resources such as a single db or network connection. A variation, sometimes called a multiton, would distribute a...
3
18255
weaknessforcats
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
5
2109
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share states. I'm thinking about this design pattern (I don't know if anyone has ever thought of this pattern before): class OddClass(object): def __init__(self): global OddClass
0
9690
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.