THE SINGLETON PATTERN?

A

Alicia Roberts

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. What issues are involved in this, and how
would you do this?

If someone knows about the Singleton pattern please reply. Thanks

Alicia
 
V

Victor Bazarov

Alicia Roberts 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. What issues are involved in this, and how
would you do this?

If someone knows about the Singleton pattern please reply. Thanks

There is this newsgroup, called comp.software.patterns. You might
want to check it out.

There is also comp.object, don't discount it too quickly.

Your questions and concerns seem a bit farther from C++ to still be
topical here... But maybe it's just me.

Also, if you have a research project given to you as an assignment at
school, typing it into a newsgroup posting doesn't constitute doing
the research. To start a discussion you need to give at least some
of your findings (if any). You do claim to "have been researching",
don't you? So, whaddya got so far? Nah, never mind. OT anyway.

V
 
T

Thomas Matthews

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
 
M

Mike Wahler

Thomas Matthews said:
Not true. A singleton does not need a filename, database, URL or
anything like that. You need to do more research.

I suspect that Alicia means that her particular application
needs a singleton which implements these things. Of course
I could be mistaken, and she might also be mistaken in thinking
that a singleton is a good solution. We can't know without
much more elaboration of here specifications.

I do think your example will be useful to many people, however.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top