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

Darrell Norton's Blog [MVP]

Fill in description here...

Singleton vs. Monostate

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.  Here is a sample Singleton implementation:

 

using System;

namespace TestLibrary

{

      public class Singleton

      {

            private Singleton()

            { }

 

            private static Singleton theInstance = new Singleton();

 

            public static Singleton instance()

            {

                  return theInstance;

            }

      }

}

 

But what about the monostate pattern?  The monostate enforces the behavior of a singleton without the structure of the monostate.  This is a sample implementation of a monostate:

 

using System;

namespace TestLibrary

{

      public class Monostate

      {

            private static int _x;

 

            public Monostate()

            { }

 

            public static int x

            {

                  get{ return _x; }

                  set{ _x = value; }

            }

      }

}

 

Here you can instantiate many Monostate objects, but the x property is the same across all objects.  What are the benefits of the monostate pattern over the singleton pattern?



Comments

Darrell said:

Ok, I fixed the code. I must have copied it incorrectly. Thanks!
# February 5, 2004 6:45 AM

Darrell said:

Yeah, the MSDN sample definitely looks better. Mine was a port from Java, so not as good!

But still, what is the advantage to the Monostate pattern?
# February 5, 2004 6:46 AM

James Avery said:

Well, I don't know if there is an advantage... perhaps more of a difference. The singleton is about limiting an object to a single instance of that object, whereas the monostate is about creating a single state that all of the instances use.

I think they both have there uses.

-James
# February 5, 2004 6:57 AM

Steve Metsker said:

Good question, and... there's some interesting discussion of (what I take to be) your point, at http://c2.com/cgi/wiki?JavaSingleton.

I personally would tend to view your Monostate class as an *implementation* of the Singleton pattern. The structure of code that fulfills the intent of a pattern is not critical -- what makes code an example or implementation of a pattern is that it fulfills the intent.

- Steve Metsker
# February 5, 2004 9:20 AM

Darrell said:

Very interesting!
# February 5, 2004 1:57 PM

Steve Maine said:

Interesting post. I agree w/ James -- I'm not sure if there is a real semantic difference.

I like the fact that singletons can't be instantiated via normal mechanisms. As you said, developers always know when they're dealing with a singleton -- because the compiler tells them. The monostate pattern gives a different semantic to object instantiation but retains the same syntax. With singletons, the difference in construction syntax reminds me of the difference in behavior.
# February 9, 2004 4:30 PM

Darrell said:

Steve Maine - Personally I would prefer a Singleton over a Monostate for the exact same reasons you mention. But when Robert Martin described the Monostate pattern in as much depth as the Singleton, I wondered what the use of it was. Probably the only time I would use it is if I had to refactor a regular class into a Singleton but didn't want to fix all those broken instantiations.
# February 10, 2004 12:17 AM

Ken said:

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.
# June 23, 2004 3:29 PM

Darrell Norton's Blog said:

Why choose monostate over singleton?
# June 24, 2004 3:43 AM

Darrell said:

Thanks Ken. That was an excellent comment!
# June 24, 2004 3:46 AM

Thomas Eyde said:

Monostate is also usually preferred if you code in a test-first fasion. That is because you sometimes want to insert a mock object into the object under test so you can test its behaviour.

That's harder to do with a singelton because you don't have a constructor to insert the mock with.
# June 24, 2004 6:57 AM

Darrell said:

Thomas - good point, though I have not had to TDD a Monostate yet. Mock objects are an important simplifying mechanism in TDD.
# June 24, 2004 7:36 AM

Robert Dexter said:

You might find situations where you still need instance data. Therefore a monostate would be better. You can create the instance, use methods to affect instance data and still have access to the static data. With a singleton, you cannot have instance data.

I also tend to think that a singleton is one way of making a set of global functions, sort of doing the C code thing, but with C++. When converting C code to C++, people tend to do this, and not redesign.
# September 9, 2004 11:27 AM

Coding in an Igloo said:

Last week I spent five full days under the tutelage of Jean-Paul Boodhoo. He was offering a course...

# February 25, 2007 7:39 PM
Check out Devlicio.us!