Exforsys

Online Training

How to use the Singleton pattern right

This is a discussion on How to use the Singleton pattern right within the Software Patterns forums, part of the Testing category; Hi everbody. I recently did my first serious implementation of the Singleton pattern and stumbled into mindboggling troubles immediately. Imagine ...


Go Back   Exforsys > Testing > Software Patterns

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-07-2003, 04:57 PM
Malte Persike
Guest
 
Posts: n/a
How to use the Singleton pattern right

Hi everbody.

I recently did my first serious implementation of the Singleton
pattern and stumbled into mindboggling troubles immediately.

Imagine the following situation: A data management system shall be
equipped with a mechanism that tracks all kinds of errors, warnings
and status messages that occur during runtime. This mechanism - let's
call it the event logger - must be accessible by virtually every
object in the system but may as well never be called at all.

I chose to plan the event logger as a singleton which looked quite
plausible to me. Here comes the problem: Who is responsible for
cleaning up the event logger when it is no longer needed?

Any reference counting approach (like Java's garbage collection) would
fatally screw up things because there will be times in which the event
logger has been created but the objects that requested it have already
been destroyed. Garbage Collection steps in, deletes the event logger
with all the accumulated messages - just before a new error occurs and
the event logger gets referenced again.

The crucial distinction here is between "stateless" and "stateful"
singletons. Stateless singletons just do something and can then well
be destroyed again, whereas stateful singletons are required to retain
a certain internal state which can be queried later.

How would you implement stateful singletons?

Kind regards,
Malte

---

The above e-mail address is not valid.
To contact me, please use my real
e-mail address:

malte AT t DASH online DOT de

Just replace the capitalized words with
the corresponding punctuation marks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-07-2003, 05:21 PM
Simon Strandgaard
Guest
 
Posts: n/a
Re: How to use the Singleton pattern right

On Tue, 07 Oct 2003 22:57:36 +0200, Malte Persike wrote:
[snip]
> The crucial distinction here is between "stateless" and "stateful"
> singletons. Stateless singletons just do something and can then well
> be destroyed again, whereas stateful singletons are required to retain
> a certain internal state which can be queried later.
>
> How would you implement stateful singletons?



In ModernC++Design there is a walkthrough though many different kinds of
singletons. The one you are talking about is a Phoenix singleton, which
is capable of respawning.
http://www.moderncppdesign.com/

You can download Loki, which is the code which comes with the book.
It contains a nice implementation of the phoenix singleton.
http://sourceforge.net/projects/loki-lib/

--
Simon Strandgaard
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-07-2003, 08:40 PM
Malte Persike
Guest
 
Posts: n/a
Re: How to use the Singleton pattern right

>You can download Loki, which is the code which comes with the book.
>It contains a nice implementation of the phoenix singleton.
>http://sourceforge.net/projects/loki-lib/



Dear Simon,

thanks a LOT (if formatting was allowed in Newsgroups, that would have
been king-size). This was helpful!

Regards,
Malte

---

The above e-mail address is not valid.
To contact me, please use my real
e-mail address:

malte AT t DASH online DOT de

Just replace the capitalized words with
the corresponding punctuation marks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-07-2003, 08:51 PM
Malte Persike
Guest
 
Posts: n/a
Re: How to use the Singleton pattern right

By the way, this newsgroup seems to be not the most active one. I
wonder why this is the case, especially when considering the relevance
of the topic for most programmers out there.
Do you have a clue?

Sorry for being somewhat off-topic,
Malte

---

The above e-mail address is not valid.
To contact me, please use my real
e-mail address:

malte AT t DASH online DOT de

Just replace the capitalized words with
the corresponding punctuation marks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-08-2003, 10:09 AM
Simon Strandgaard
Guest
 
Posts: n/a
Re: How to use the Singleton pattern right

On Wed, 08 Oct 2003 02:51:15 +0200, Malte Persike wrote:

> By the way, this newsgroup seems to be not the most active one. I
> wonder why this is the case, especially when considering the relevance
> of the topic for most programmers out there.
> Do you have a clue?


There is also much talk about patterns on comp.object
and on my favorite newsgroup comp.lang.ruby

I guess people more often ask questions in the language specific
newsgroups, like how do I implement observer-pattern in 4004 assembler.

Agree, more activity in this forum would be interesting :-)

--
Simon Strandgaard

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-08-2003, 10:08 PM
Malte Persike
Guest
 
Posts: n/a
Re: How to use the Singleton pattern right

>Agree, more activity in this forum would be interesting :-)

So let's get it on ;-)

---

The above e-mail address is not valid.
To contact me, please use my real
e-mail address:

malte AT t DASH online DOT de

Just replace the capitalized words with
the corresponding punctuation marks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-10-2003, 03:54 PM
William Stacey
Guest
 
Posts: n/a
Re: How to use the Singleton pattern right

> I'd suggest managing the state outside of the singleton itself. If you
> create the singleton as a stateless entity and then delegate stateful
> operations elsewhere, you'll have fewer problems with concurrent access,
> supporting higher load, and issues with distributed systems.


Thanks. Any examples of this?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:09 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.