Darrell Norton's Blog [MVP]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.”


Posted Thu, Jun 24 2004 7:43 AM by Darrell Norton

[Advertisement]

Comments

Alex wrote re: Why choose monostate over singleton?
on Thu, Jun 24 2004 4:15 AM
So how would you implement this pattern (monostate) in code?
Darrell wrote re: Why choose monostate over singleton?
on Thu, Jun 24 2004 4:59 AM
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.
Ron Scott wrote re: Why choose monostate over singleton?
on Thu, Jul 1 2004 6:06 AM
Ron Scott wrote re: Why choose monostate over singleton?
on Thu, Jul 1 2004 6:06 AM
Darrell wrote re: Why choose monostate over singleton?
on Thu, Jul 1 2004 6:14 AM
Ron, excellent link. Very clear and thorough.
Devlicio.us