468,720 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

THE SINGLETON PATTERN?

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
Jul 22 '05 #1
3 2214
"Alicia Roberts" <al******@hotmail.com> wrote...
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
Jul 22 '05 #2
Alicia Roberts wrote:
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

Jul 22 '05 #3
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:41**************@sbcglobal.net...
Alicia Roberts wrote:
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 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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alicia Roberts | last post: by
4 posts views Thread by Neil Zanella | last post: by
11 posts views Thread by Daniel Billingsley | last post: by
13 posts views Thread by Robert W. | last post: by
2 posts views Thread by Kevin Newman | last post: by
3 posts views Thread by dischdennis | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.