Singleton desin pattern in C++

A

Alfonso Morra

Hi,

Anyone knows of a link to a singleton design pattern example in C++
(preferably not using a global variable?).
 
?

=?iso-8859-1?q?St=E9phane_Wirtel?=

About the Singleton, where can I find a good example in a
multithreading environment without the Double Checked Locking Pattern ?

Is there a solution to this problem ?
 
A

Alfonso Morra

Victor said:

Really?, I never thought of that. Seriously though, I have already
searched google on several occasions in the past (including this
morning), there are lots of examples available in Java - but I cannot
seem to locate anything on C++ (-that is, anything that goes beyond the
definition of what a design pattern is etc..).

A singleton is a particularly simple design (if not the simplest), but
rather than rush ahead and "roll my own", I thought I'd find out if an
implementation is already available, from someone who (hopefully) has a
lot more experience in C++ than I do.
 
A

Alfonso Morra

Max said:
If you're interested in a real-world implementation, you may want to take a
look at ACE's source code: http://www.cs.wustl.edu/~schmidt/ACE.html.

Max

I did look at this several moons ago (2 years to be exact), and I got
slightly initimidated by all the CORBA specific (as well as advanced C++
stuff), I think I'll stay away from it for a while - I want to keep
things relatively simple for now ...

but thanks for your time anyhows ...
 
M

Mercator

Alfonso said:
I did look at this several moons ago (2 years to be exact), and I got
slightly initimidated by all the CORBA specific (as well as advanced C++
stuff), I think I'll stay away from it for a while - I want to keep
things relatively simple for now ...

Singletons are merely global variables. And global variables are evil.
It doesn't make them any better to call them 'Singleton design pattern'.
 
O

osmium

Alfonso Morra said:
Anyone knows of a link to a singleton design pattern example in C++
(preferably not using a global variable?).

Here's a couple links I saved some time back, I don't certify them but they
passed my "I don't like this link" test.

http://www.codeguru.com/Cpp/Cpp/cpp_mfc/singletons/article.php/c823
http://www.csc.calpoly.edu/~dbutler/tutorials/winter96/patterns/tutorial.html

I had a third one but it is no longer on the Web.

Depending on how fussily you define global variable, you might be trying to
do the impossible. But I note you don't insist on that aspect.
 
M

Max M.

Alfonso said:
I did look at this several moons ago (2 years to be exact), and I got
slightly initimidated by all the CORBA specific (as well as advanced C++
stuff), I think I'll stay away from it for a while

There's definitely no CORBA-specific code in the ACE core library and the
singleton implementation is spread across a few files, but I concede
there's probably little point in studying all the dependencies if you don't
plan to use the library.

Max
 
S

Stuart MacMartin

Singletons are merely global variables. And global variables are evil.
It doesn't make them any better to call them 'Singleton design pattern'.

Are you saying this to be inflammatory or just to show your lack of
understanding? To me this sounds like a flippant silly comment. Why
did you say it?

Stuart
 
V

Vijai Kalyan

You might not want to use ACE at all, but it may still be worthwhile to
look through it. There are a few (couple of hundred slides :)
presentations on ACE and patterns etc from one of the courses that Dr.
Schmidt teaches at Vanderbilt (look up Component Middleware or
Operating Systems - II). If you have questions (not trivial) you might
actually want to contact Dr. Doug Schmidt. I am sure he will be able to
answer a couple of important questions.

-vijai.
 
M

Mercator

Stuart said:
Are you saying this to be inflammatory or just to show your lack of
understanding? To me this sounds like a flippant silly comment. Why
did you say it?

And the difference is??? Don't blather, try it in one sentence.
 
B

Ben Pope

Alfonso said:
A singleton is a particularly simple design (if not the simplest), but
rather than rush ahead and "roll my own", I thought I'd find out if an
implementation is already available, from someone who (hopefully) has a
lot more experience in C++ than I do.

After reading some of Alexandrescus' Modern C++ Design, I can assure you that it's not the simplest design by far!

Check out
http://safari.informit.com/?x=1&mode=section&sortKey=title&sortOrder=asc&xmlid=0-201-70431-5/ch06

And Loki.

Ben
 
M

msalters

Mercator schreef:
Singletons are merely global variables. And global variables are evil.
It doesn't make them any better to call them 'Singleton design pattern'.

Wrong. Or do you really think that the single global heap used by new
is evil? Should each use of new/delete require a (matching) heapID?
Wait a minute, that would be one global array of heaps - again a
singleton. See, there is really no escape.

Basically, if my design tells me that a certain entity occurs only
once, then that is a Singleton. You can't fool reality.

HTH,
Michiel Salters
 
M

Mercator

msalters said:
You can't fool reality.

At least you are right here. A one-instance Singleton global variable
produces the same coupling as ordinary multi-instance global variables.
 
B

Bart

Mercator said:
At least you are right here. A one-instance Singleton global variable
produces the same coupling as ordinary multi-instance global variables.

And why do you think such coupling is necessarily evil? Convoluted
impractically "flexible" designs are also evil.
 
M

Mercator

Bart said:
And why do you think such coupling is necessarily evil?
Yes!

Convoluted impractically "flexible" designs are also evil.

Avoid flexibility by using global Singletons?? I beg to differ!
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top