473,385 Members | 1,912 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,385 software developers and data experts.

Prevalence of Singleton

Just wondering the extent to which some of you are implementing classes as
Singletons. I'm working on a brand new project and, early on, identified
some obvious candidates. By "obvoius candidates" I mean classes for which
terrible problems would clearly arise if more than one instance were to
exist. But as I'm getting into the design of this new solution, I'm
realizing that a large percentage of the classes _could be_ implemented as
Singletons. These are classes for which there is no reasonable expectation
to ever need more than one instance at a time.

And yes - I understand that there is no definitive answer to the question
and that it should be answered on a case-by-case basis.

So I'm asking about some of the guidelines you are using as you make the
decision - Singleton or not.

Thanks.
Jul 25 '07 #1
2 1562
On Wed, 25 Jul 2007 12:56:53 -0700, Bob Johnson <A@B.comwrote:
Just wondering the extent to which some of you are implementing classes
as
Singletons. I'm working on a brand new project and, early on, identified
some obvious candidates. By "obvoius candidates" I mean classes for which
terrible problems would clearly arise if more than one instance were to
exist. But as I'm getting into the design of this new solution, I'm
realizing that a large percentage of the classes _could be_ implemented
as
Singletons. These are classes for which there is no reasonable
expectation
to ever need more than one instance at a time.
I use it when it's appropriate, where "appropriate" is defined by my own
fickle criteria. :)

I have to say, the decision pretty much _never_ comes down to a question
of whether "terrible problems would clearly arise if more than one
instance were to exist". By that I mean that if that was the case, the
singleton pattern was already suggested earlier by some other design
criteria.

In particular, to me the question of whether to use the singleton or not
is whether I'm dealing with a class that will represent multiple
instances, each with their own state, or I'm dealing with a class that
naturally lends itself to being a singleton.

The decision flow typically just has a couple of questions: is this class
naturally a singleton? and is it worth bothering to make it a singleton?
If something terrible might happen when more than one instance exists,
then the answer to the first question is already "yes" and I never get as
far as considering what terrible things might happen.

Of course, not all singleton classes would actually cause problems being
multiply instanced...they'd just be wasteful. So that's where the second
question comes in. There's a certain amount of hassle involved in making
a class a singleton. Not much, of course, but it does create at least a
little complication. So for a really simple class, I would just not
bother.

For example, something that came up for me recently was the need to
implement the IComparer interface. I wanted a brand new class for this,
as I didn't have a class already that naturally lent itself to the task.
I mean, I could've just implemented the interface in my main form class,
for all the Sort() method cared, but it wasn't a perfect fit in that
particular case, so I decided against that. But the new class I made
didn't have _any_ member fields. All it did was implement the one
required method, IComparer.Compare().

At the same time, it seemed silly to instantiate one of these each time I
wanted to compare something, especially when I wanted to do the comparison
in situations where I wasn't actually using the IComparer interface (that
is, sometimes I needed to pass an IComparer to something, like Sort(), but
in other cases it was my own code doing the comparison and so I didn't
need to require an IComparer instance). I _could_ have addressed all of
this by making the class a singleton.

But I didn't. I just made a static comparison method, which was callable
by any arbitrary code (i.e. my own code that was aware of the class
itself), and which was called by the instance method IComparer.Compare().
That way, I didn't even need an instance in most cases. When I did need
an instance, I went ahead and just made one, not worrying about whether
having multiple instances around would be a problem, even though they
would be redundant.

The only theoretical problem would have been memory consumption, which
isn't much of an issue when your class contains no member fields and when
you have single-threaded coded in which the instance exists for only brief
periods of time. In fact, in some respects this is better than using a
singleton, because the class instance exists only when it's actually
needed, and will be automatically be released if the memory needs to be
reclaimed when it's not in use. Yes, I gather an alternative solution
involves using a WeakReference with the singleton pattern, but that's even
more complicated, and one of the highest priority design criteria I have
is to keep the code simple.

So, in spite of all of the words, I think the sum-up is what I wrote at
the outset. If a class is a naturally a singleton, _and_ it is simpler to
implement it as a singleton than to work around its singleton nature some
other way, then I implement it as a singleton. Otherwise I don't.

Pete
Jul 25 '07 #2
In addition to what Peter D. has enlightened us with, you may find it helpful
to review Sir Jon's treatise on the Singleton pattern, which I have found
most instructive:
http://www.yoda.arachsys.com/csharp/singleton.html

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com

"Bob Johnson" wrote:
Just wondering the extent to which some of you are implementing classes as
Singletons. I'm working on a brand new project and, early on, identified
some obvious candidates. By "obvoius candidates" I mean classes for which
terrible problems would clearly arise if more than one instance were to
exist. But as I'm getting into the design of this new solution, I'm
realizing that a large percentage of the classes _could be_ implemented as
Singletons. These are classes for which there is no reasonable expectation
to ever need more than one instance at a time.

And yes - I understand that there is no definitive answer to the question
and that it should be answered on a case-by-case basis.

So I'm asking about some of the guidelines you are using as you make the
decision - Singleton or not.

Thanks.
Jul 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
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...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
9
by: Christopher Benson-Manica | last post by:
In your experience, what is the prevalence of "good" C++ (i.e., the code makes full use of solid language features and the STL) versus C++ written in ignorance (or defiance) of one or more key...
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...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.