C++
  Home arrow C++ arrow Page 3 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 - Policy-based Class Design


    (Page 3 of 5 )

    In brief, policy-based class design is used to assemble a class out of little classes (called policies), each of which takes care of only one behavioral or structural aspect. If you use the Standard Template Library, you are probably already familiar with std::string. This is a typedef for the std::basic_string class which accepts char_traits as one of its template parameters. Policies are very much like traits, with the main difference being that they focus on behavior rather than type.

    With the Singleton class, the creation process of the object that has to become the singleton has been extrapolated into policy classes. The reason for this is that there might be different reasons to construct the object in different ways.

    Because you can mix and match policies, you can achieve a combinatorial set of behaviors by using only a small core of elementary components. The downside of this is that what used to be declared and defined in a single class is now scattered over multiple declarations and definitions.

    For someone looking at that code for the first time, it can become a bit hard to understand what is going on (especially if that someone hasn’t seen or used policies before). On the other hand, I won’t claim that writing the same class over and over again while only tweaking the pieces that could go into a policy is a better solution. If you write your code like that it won’t be more intelligible -- worse, it will be a lot more sensitive to bugs! (Not to mention the fact that changing and fixing the code becomes tedious, since you will have to make changes to a lot of classes, when you could have handled it with just the class and a couple of policies.)

    You can see in the singleton code that there are two policies given for the creation of an object inside the singleton class (CreateLeaking and CreateMeyers). When you declare the Singleton you provide the creation policy you like as a template parameter (e.g. typedef Singleton<MyClass, CreateLeaking> MySingleton). Next when Singleton::Instance() is called, it uses the Create function stored in that policy (i.e. CreationPolicy<T>::Create()). So in the CreateLeaking case we end up executing:

    static T& Instance() {

    if (!m_pInstance)

    m_pInstance = CreateLeaking<T>::Create();

    return *m_pInstance;

    }

    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

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT