Click here to Skip to main content
Licence 
First Posted 19 Apr 2007
Views 8,130
Downloads 42
Bookmarked 8 times

A implemetion of Singleton Pattern

By | 19 Apr 2007 | Article
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

About the Author

Bony Chen

Web Developer

China China

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhere's the description? PinmvpHans Dietrich5:09 20 Apr '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Apr 2007
Article Copyright 2007 by Bony Chen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid