C++
  Home arrow C++ arrow Page 4 - The Singleton Pattern Revisited
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

The Singleton Pattern Revisited
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-05-02

    Table of Contents:
  • The Singleton Pattern Revisited
  • The creation of a Singleton
  • Policy-based Class Design
  • Tweaking the Singleton Class with Policies
  • Singleton's Thread Safety

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By J. Nakamura


       · The "Double-Checked Locking Pattern" is broken per Meyers and Alexandrescu....
       · I may miss something here, but when usingif (!m_instance){ Guard...
       · The m_Mutex is intended to be static, thus it is initialised outside the class. I am...
       · Earlier you used this[b]Guard lock(m_mutex);[/b]But in [i]Double-Checked...
     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT