Home arrow C++ arrow Page 3 - C++ In Theory: The Singleton Pattern, Part 2
C++

C++ In Theory: The Singleton Pattern, Part 2


In our second article on the Singleton pattern, J. Nakamura discusses a reusable code version of the pattern.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 41
January 25, 2005
TABLE OF CONTENTS:
  1. · C++ In Theory: The Singleton Pattern, Part 2
  2. · Singleton Generalization
  3. · Testing Our Generic Implementation
  4. · Singletons are not for Simpletons

print this article
SEARCH DEVARTICLES

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.


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