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.
Next: Singletons are not for Simpletons >>
More C++ Articles
More By J. Nakamura