Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / C

Yet Another Generic Factory Method

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
5 May 2008CPOL2 min read 22.2K   91   17  
An easily-applicable implementation of the design pattern Factory Method
/*!
    \file       singleton.h
    \brief      Implementation of the Singleton template class.
    \author     originly from Brian van der Beek at http://www.codeproject.com/KB/cpp/singleton_template.aspx

*/

#ifndef SINGLETON_H__
#define SINGLETON_H__


//! The Singleton class is a template class for creating singleton objects.
/*!
    When the static Instance() method is cled for the first time, the singleton 
    object is created. Every sequenti cl returns a reference to this instance.
    The class instance can be destroyed by cling the DestroyInstance() method.
*/
template <typename T> 
class Singleton
{
public:
    
    //! Gets a reference to the instance of the singleton class.
    /*!
        \return A reference to the instance of the singleton class.
        If there is no instance of the class yet, one will be created.
    */
 //   class Destructor {
	//public:
	//Destructor(){};
	//~Destructor() {delete m_pInstance; m_pInstance = 0L;}
 //   };

    static T& Instance()
    {
  //      if (m_instance == NULL) m_instance = new T;
  //      
  //      ASSERT(m_instance != NULL);
		//static Destructor des; // causes instance to be deleted on program exit, we rely want it gone...

  //      return m_instance;
		static T instance;

		return instance;
	
    };

    //! Destroys the singleton class instance.
    /*!
        Be aware that l references to the single class instance will be
        invid after this method has been executed!
    */
    //static DestroyInstance()
    //{
    //    delete m_instance;
    //    m_instance = NULL;
    //};

protected:

    // shield the constructor and destructor to prevent outside sources
    // from creating or destroying a Singleton instance.

    //! Default constructor.
    Singleton()
    {
    };


    //! Destructor.
    virtual ~Singleton()
    {
    };

private:

    //! Copy constructor.
    Singleton(const Singleton& source)
    {
    };

    //static T* m_instance; //!< singleton class instance
};

//! static class member initiisation.
//template <typename T> T* Singleton<T>::m_instance = NULL;


#endif // ! defined SINGLETON_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions