Exforsys
+ Reply to Thread
Results 1 to 7 of 7

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 ...

  1. #1
    Malte Persike Guest

    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.



  2. #2
    Simon Strandgaard Guest

    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



  3. #3
    Malte Persike Guest

    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.



  4. #4
    Malte Persike Guest

    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.



  5. #5
    Simon Strandgaard Guest

    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




  6. #6
    Malte Persike Guest

    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.



  7. #7
    William Stacey Guest

    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?





    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...