Alicia said:
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic what sort of
problems/issues should be considered?
Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc.
Not true. A singleton does not need a filename, database, URL or
anything like that. You need to do more research.
I've used many singletons that didn't have a filename, database or
URL.
What issues are involved in this, and how would you do this?
I wouldn't use an object that requires a filename or database or
URL unless the application required it.
If someone knows about the Singleton pattern please reply. Thanks
Alicia
Check out Scott Meyer's "Effective C++" and "More Effective C++".
Here is an example:
#ifndef SINGLETON_HPP
#define SINGLETON_HPP
//! \file singleton.hpp
/*! This file contains the declaration & implementation of the
* Common::Singlton class.
*<hr>\n
*<center>Copyright (C) 2001 - 2004, Thomas Matthews
*
*PROPRIETARY NOTICE
*
*This document is the property of Thomas Matthews, with the\n
*information herein reserved as proprietary to Thomas Matthews,\n
*and is not to be published, reproduced, copied, disclosed,\n
*or used without the express written consent of a duly\n
*authorized representative of Thomas Matthews.</center>\n
*
* Description:\n
* This class represents the Singleton Design Pattern.\n
*
* Notes:\n
* 1. A singleton allows only one instantiation of a class.\n
*
* Usage:\n
* #include "singleton.hpp"\n
* using namespace Common;\n
* class My_Class\n
* : public Singleton<My_Class>\n
* {\n
* friend class Singleton<My_Class>;\n
* protected:\n
* My_Class();\n
* };\n
*
* Recent History {most recent first}:\n
* 22 Jul 2004 TOM Converted comments to Doxygen format.\n
*
* Extended history at end of file.\n
*<hr>\n
*/
/*! The Common namespace is for patterns and functions usable by many
* projects.
*/
namespace Common
{
//! Singleton Design Pattern
/*! The Singleton class implements the Singleton Design Pattern.
* This pattern ensures that only one instance of the class will exist
* during the program life time.
* \author Thomas O. Matthews
*/
template <class Target>
class Singleton
{
//---------------------------------------------------------------------
// Public Constructors & Destructors
//---------------------------------------------------------------------
public:
//! Destructor.
virtual ~Singleton();
//---------------------------------------------------------------------
// Public methods
//---------------------------------------------------------------------
public:
//! Returns a pointer to the instance.
static Target * ptr(void);
//! Returns a reference to the instance.
static Target & ref(void);
//---------------------------------------------------------------------
// Protected methods
//---------------------------------------------------------------------
protected:
//! Default constructor.
Singleton();
//---------------------------------------------------------------------
// Private methods
//---------------------------------------------------------------------
private:
//! Copy constructor, not implemented.
/*! The copy constructor is declared so that the compiler will not
* automatically generate one.
*/
Singleton(const Singleton& s);
//! Assignment operator, declared but not defined.
/*! The assignment operator is declared so that the compiler will
* not automatically generate one.
*/
Singleton& operator=(const Singleton& s);
};
//-------------------------------------------------------------------------
// Singleton Constructors & Destructors
//-------------------------------------------------------------------------
template <class Target>
inline
Singleton<Target> ::
Singleton()
{
}
template <class Target>
inline
Singleton<Target> ::
~Singleton()
{
}
//-------------------------------------------------------------------------
// Singleton methods in alphabetical order
//-------------------------------------------------------------------------
template <class Target>
Target *
Singleton<Target> ::
ptr(void)
{
return &(ref());
}
template <class Target>
Target &
Singleton<Target> ::
ref(void)
{
//! The actual and only instance of the target class.
/*! Static variables declared in a function are guaranteed to be
* initialized upon the first execution of the function.
* This also ensures that only one instance will exist.
*/
static Target the_instance;
return the_instance;
}
} // End namespace: Common
#endif // SINGLETON_HPP
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library