473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2456
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.