C++
  Home arrow C++ arrow Page 3 - C++ In Theory: The Singleton Pattern, Part...
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 
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++

C++ In Theory: The Singleton Pattern, Part 2
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2005-01-25

    Table of Contents:
  • C++ In Theory: The Singleton Pattern, Part 2
  • Singleton Generalization
  • Testing Our Generic Implementation
  • Singletons are not for Simpletons

  • 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


    C++ In Theory: The Singleton Pattern, Part 2 - Testing Our Generic Implementation


    (Page 3 of 4 )

    Let's run a little test to trace object construction and destruction when we use our generic singleton implementation. We expect our singleton class to be created only once!

    // main.cpp
    #include <stdio.h>
    #include “singleton.h”

    class MyClass {
    public:
    MyClass()
    : m_Value(0)
    , m_ID(++sCount)
    {
      (void)printf(“MyClass #%d constructed.\n”, m_ID); 
    }

    ~MyClass() {
    (void)printf(“MyClass #%d destructed.\n”, m_ID);
    }

      void foo(int value) {
    (void)printf(“MyClass%d::foo(0x%08X) called.\n”, m_ID, value);
    m_Value=value;
    }

      int bar() const
      {
        (void)printf(“MyClass%d::bar() called.\n”, m_ID);
        (void)printf(“… m_Value is: 0x%08X.\n”, m_Value);
        return m_Value;
    }

    private:
      int m_Value;
      int m_ID;
      static int sCount;
    };

    int MyClass::sCount=0;

    typedef Singleton<MyClass> MyClassSingleton;

    void test1() {
      MyClass myObj;
      int val=myObj.bar();
      myObj.foo(++val);
      (void)myObj.bar();
    }

    void test2() {
      int val=MyClassSingleton::Instance().bar();
      MyClassSingleton::Instance().foo(++val);
      (void)MyClassSingleton::Instance().bar();
    }

    int main(int argc, char *argv[]) {
      (void)printf(“*** test1 ***\n”);
      test1();
      (void)printf(“*** test1 ***\n”);
      test1();

      (void)printf(\n*** using singleton ***\n”);
      MyClassSingleton::Instance().foo(0x0010);
      (void)printf(“*** test2 ***\n”);
      test2();
      (void)printf(“*** test2 ***\n”);
      test2();

      (void)printf(“\n--- DONE ---\n”);
      return 0;
    }

    I defined a MyClass trace object that prints a message when it is constructed and when it is destructed. On top of that, every instantiated MyClass receives an unique ID, so that we can see which instantiated object is being manipulated.

    Running the code above will give you the following result:

    *** test1 ***
    MyClass #1 constructed.      #start lifetime MyClass #1 (normal)
    MyClass1::bar() called.
    ... m_Value is: 0x00000000.
    MyClass1::foo(0x00000001) called.
    MyClass1::bar() called.
    ... m_Value is: 0x00000001.
    MyClass #1 destructed.      #end lifetime MyClass #1
    *** test1 ***          #.. we create another one
    MyClass #2 constructed.      #start lifetime MyClass #2 (normal)
    MyClass2::bar() called.
    ... m_Value is: 0x00000000.
    MyClass2::foo(0x00000001) called.
    MyClass2::bar() called.
    ... m_Value is: 0x00000001.
    MyClass #2 destructed.      #end lifetime MyClass #2

    *** using singleton ***
    MyClass #3 constructed.      #start lifetime MyClass #3 (singleton)
    MyClass3::foo(0x00000010) called.    #constructed before we call test()
    *** test2 ***
    MyClass3::bar() called.
    ... m_Value is: 0x00000010.
    MyClass3::foo(0x00000011) called.
    MyClass3::bar() called.
    ... m_Value is: 0x00000011.

    *** test2 ***          #and it stays alive!
    MyClass3::bar() called.
    ... m_Value is: 0x00000011.
    MyClass3::foo(0x00000012) called.
    MyClass3::bar() called.
    ... m_Value is: 0x00000012.

    --- DONE ---
    MyClass #3 destructed.      #singleton destroyed at exit!

    Our Singleton template has provided the means to transform MyClass into a Singleton, following the definition/intent as provided in “Design Patterns” [Gamma]: “Ensure a class only has one instance, and provide a global point of access to it.” Great, isn’t it? Well it is...until you start using multiple Singletons that depend on each other.

    More C++ Articles
    More By J. Nakamura


       · Very nice. But one question from dummy-programmer: what, if my class has a...
       · If you want your singleton to use parameters in its construction, for example...
       · A good introduction, but for the serious more advanced programmer, you can introduce...
     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - 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






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