Home arrow C++ arrow Page 4 - The Singleton Pattern Revisited
C++

The Singleton Pattern Revisited


A reader wrote in some time after we ran a short series on the Singleton pattern with some questions. These included how to construct a singleton when the class has a non-trivial constructor with arguments that are calculated at run-time, and how to use the singleton in a multi-threaded environment. If you were wondering about these issues as well, keep reading.

Author Info:
By: J. Nakamura
Rating: 4 stars4 stars4 stars4 stars4 stars / 19
May 02, 2005
TABLE OF CONTENTS:
  1. · The Singleton Pattern Revisited
  2. · The creation of a Singleton
  3. · Policy-based Class Design
  4. · Tweaking the Singleton Class with Policies
  5. · Singleton's Thread Safety

print this article
SEARCH DEVARTICLES

The Singleton Pattern Revisited - Tweaking the Singleton Class with Policies
(Page 4 of 5 )

Have you guessed how we can use the Singleton class for our case with the constructor that takes arguments? The question has already been answered; it can all be solved by implementing a specific singleton construction policy. We introduce a new creation policy!

Presume that a class takes an int and a char const * as arguments in the constructor; here is how we could implement our creation policy in the [Gamma] fashion:

template <class T> struct CreateClassXSingleton {

static T* Create() {

assert(NULL != m_strName);

return new T(m_nSize, m_strName);

}

static int m_nCount;

static char const *m_strName;

};

template <class T>

int CreateClassXSingleton<T>::m_nCount = 0;

template <class T> char const* CreateClassXSingleton<T>::m_strName = NULL;

The one thing you have to make sure of is that the static member variables of the creation policy class are set before you call Instance() on the singleton. That is why I put assert in there.

At the moment I am thinking about the possibilities of generalizing this further, but most solutions involve the need to make changes to the class we want to use as a singleton. Please feel free to drop me an email if you find a more generic solution.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials