472,958 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2432
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alicia Roberts | last post by:
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...
4
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be...
1
by: Richard A. DeVenezia | last post by:
foo() generates elements with event handlers that invoke foo function properties. Is this an abhorrent or misthought pattern ? It allows just the one occurence of identifier /foo/ to be changed...
11
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be...
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.