CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Darrell Norton's Blog [MVP]

Fill in description here...

Why choose monostate over singleton?

A while back I was wondering what the pragmatic difference was between singleton and monostate. Recently someone named Ken left a comment that described why someone would use monostate over singleton other than personal preference.

To recap, the singleton pattern structurally enforces the fact that you can never have more than one instance of a class at a time, and it is obvious to the developers that they are dealing with a singleton. On the other hand, the monostate pattern enforces the behavior of a singleton without the structure of a singleton, e.g., the singleton behavior is encapsulated from the developers.

So why choose one over the other?

“If you plan on deriving classes from the singleton and you want those classes to be singletons, your better choice is monostate. That's because all classes derived from a monostate are monostates. Classes derived singleton classes are not singletons by default. You would have to add the static method and attribute to each derived class.”



Comments

Darrell said:

Alex - check out the original post I linked to here:
http://dotnetjunkies.com/WebLog/darrell.norton/archive/2004/02/05/6644.aspx

It shows how to implement Singleton and Monostate.
# June 24, 2004 4:59 AM

Ron Scott said:

# July 1, 2004 6:06 AM

Ron Scott said:

# July 1, 2004 6:06 AM

Darrell said:

Ron, excellent link. Very clear and thorough.
# July 1, 2004 6:14 AM
Check out Devlicio.us!