Click here to Skip to main content
15,908,661 members
Articles / Programming Languages / C++
Article

A implemetion of Singleton Pattern

Rate me:
Please Sign up or sign in to vote.
1.25/5 (7 votes)
19 Apr 2007 19.2K   87   9   1
Singleton Pattern

Introduction

When you implement your application system, you may find some objects that should have only one instance throughout the application lifetime. You can implement it using global variable, but it is difficult to manage many global variables in application. Singleton Pattern is a good choice. the aim of the pattern is to ensure a class only has one instance, and provide a global point of access to it.In this article I will describe a simple singleton implementations

How is it working

template <class TInstance>
class CSingletonModel 
{
// forbid to new CSingletonModel directly
protected:
CSingletonModel()
{
    printf("CSingletonModel");
};
virtual ~CSingletonModel() 
{ 
    printf("~CSingletonModel"); 
};
// forbid to copy construct function
private:
CSingletonModel(const CSingletonModel &copy);
CSingletonModel & operator = (const CSingletonModel &copy);



protected:
//the only instance of class
static TInstance * m_pInstance;
protected:
//initialize instance
virtual BOOL InitInstance(){ return TRUE; }
//the event function before the GetInstance() return
virtual BOOL AfterGetInstance() { return TRUE; }
public:
//get the only instance
static TInstance * GetInstance()
{
//create the instance
    if (m_pInstance == NULL)
    {    
        m_pInstance = new TInstance; 
//init object.
        if (!m_pInstance->InitInstance())
        {
            delete m_pInstance;
            m_pInstance = NULL;
            return (m_pInstance);
        }
    } 
    m_pInstance->AfterGetInstance();
    return (m_pInstance);
}
//destroy the only Instance
static void DestroyInstance()
{
    if (m_pInstance)
    {
        delete m_pInstance;
        m_pInstance = NULL;
    }
};

template <class TInstance>
TInstance* CSingletonModel<TInstance>::m_pInstance = NULL;

Using the code

class CTest:public CSingletonModel<CTest>
{
public:
 CTest()
 {
  printf("CTest");
 };
 ~CTest()
 {
  printf("~CTest");
 };
public:
 virtual BOOL InitInstance()
 {
  return TRUE;
 }
 virtual BOOL AfterGetInstance()
 {
  return TRUE;
 }
};
int _tmain(int argc, _TCHAR* argv[])
{
//the first method
 CTest *pTest1 = CTest::GetInstance();
 if(pTest1)
 {
  printf("%s","get the only instance of class 1");
 }
 CTest::DestroyInstance();
//the second method
 CTest *pTest2 = CSingletonModel<CTest>::GetInstance();
 if(pTest2)
 {
  printf("%s","get the only instance of class 2");
 }
 CSingletonModel<CTest>::DestroyInstance();
 return 0;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
Bony Chen, a senior software engineer, specializes in C++, COM, C#, ASP.net, ISAPI. He has nearly seven years of software development experience, and has successfully developed many influential software products, such as SPES, CSO, QQ (a famous IM tool in China).
And now he is focusing on P2P technology. He aims at being an expert in software development, software consulting, software components and computer science.
If you have any opinions or questions in software area, please contact bonyren@163.com.

Comments and Discussions

 
QuestionWhere's the description? Pin
Hans Dietrich20-Apr-07 5:09
mentorHans Dietrich20-Apr-07 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.