flow.
"Flow is a condition of deep, nearly meditative involvement." - Tom DeMarco

The Monostate Pattern

Monday, April 21, 2008 12:49 PM

At a presentation on mock objects this past weekend, I slipped into my normal rant regarding the inherit evils of Singletons.  This inevitably lead into a brief discussion of the Monostate pattern as an alternative.  Since then, I've received a few questions requesting more information on this pattern and I've been a little surprised to find that there doesn't seem to be much accessible information on it spread across the internet.  Hopefully we can remedy that a bit with this post...

What's wrong with the Singleton

I'll spare everyone the full breadth of my rant regarding my issues with the Singleton pattern, as there are plenty others out there who can state them much more eloquently than I.  However, in the interest of context, here's a brief run down of the issues...

  • The Singleton leans heavily on public, static members in order to get its job done.  Public static members undo many of the benefits of true object oriented design.  Specifically, they can't be overridden which pretty much takes inheritance and polymorphism out of the picture. singletonChain
  • The Singleton is simply a glorified a global variable which introduces unnecessary coupling between objects.  Imagine your objects as each of series of loosely connected links in a chain, each of which only talks to its immediate neighbors yet still maintains a flexible, yet firm, linkage.  Then bunch each of those links directly on top of one another and drive a stake through the middle of them, irreversibly pinning them together forever.  That stake is the Singleton.
  • Since they're static, they can't be mocked with most mocking frameworks.  This can make proper isolated unit testing of components very difficult.   This is aggravated by the fact that Singletons are most often reserved for objects which will be simultaneously in use by multiple client objects.  This means that your unit tests will also be danger of being dependent on the state that the Singleton object was left in by another unit test if your tests are not properly isolated.

Notice that all of the issues with Singletons are related purely to their implementation.  The issue of shared state isn't really a problem.  In fact, there are many cases where the concept of shared state makes perfect sense, just not the static way we must go about it.  What we need, is a way to accomplish the same goals as the Singleton, without the headaches that come along with it.

Enter the Monostate Pattern

The Monostate pattern is just the answer we need.  The Monostate gives us the singularity of state that we so treasure in the Singleton, but without all of the static headaches that come along with it.  Let's take a look at the following class...

    public class MenuController : IMenuController
{
private int _activeMenuIndex;

public int ActiveMenuIndex
{
get { return _activeMenuIndex; }
set { _activeMenuIndex = value; }
}

public int GetValueOfCurrentMenu()
{
// Fake value, just for example... return 10;
}
}

This class represents a menu controller for an application, which is commonly implemented as a Singleton.  This class has nothing to it with the exception of a property containing the currently active menu and a (fake) method which will return any value associated with the currently active menu.  The reason we typically only want one instance of this class floating around our application is because we don't want multiple controllers fighting for control of a single menu system.  As stated above, normally this class would be a Singleton, but let's try it with a Monostate instead...

    public class MonostateMenuController : IMenuController
{
private static MenuController _menuController;

static MonostateMenuController()
{
_menuController = new MenuController();
}

public int ActiveMenuIndex
{
get { return _menuController.ActiveMenuIndex; }
set { _menuController.ActiveMenuIndex = value; }
}

public int GetValueOfCurrentMenu()
{
return _menuController.GetValueOfCurrentMenu();
}
}

 

This class encapsulates the actual MenuController class, but does it in such a manner that clients of the class are guaranteed to always work with the same underlying instance of MenuController.  Let's discuss a few key parts of this class...

  1. Both this class and the MenuController class implement the IMenuController interface.  This isn't completely necessary, but if you are comfortable coding to interfaces instead of concrete classes then this is a great way to shield clients of your class from the fact that they are actually working with a proxy to the MenuController instance, and not the actual MenuController instance itself. 
  2. Notice that this class contains both a static instance to _menuController as well as a static constructor.  The key to static members is that they are shared amongst all instances of the class (remember that the static keyword is actually shared in Visual Basic).  In this case, that means the _menuController instance will be shared across all instances of MonostateMenuController and the static constructor will only ever be executed once.
  3. Finally, notice that all of the methods implementing the IMenuController interface simply delegate to the static _menuController field.  This ensures that no matter how many instance of MonostateMenuController are created, when their methods are called the result will always come from the same instance of MenuController.  It this fact which guarantees the singularity of state which we strive for with the Singleton.

A key point to remember here is that the Monostate pattern is actually implemented as a family of classes, not a single class as is the case with the Singleton pattern.  This does lead to a slight increase of complexity on side of your implementation code.  However, your client code is actually simplified as the Monostate class is instantiated just the same as any other class, as is seen below...

            IMenuController menuController = new MonostateMenuController();
menuController1.ActiveMenuIndex = 5;
 

Summary

Using this technique, we can reap all of the benefits that the Singleton pattern gives us, without the headaches that generally come along with them.  We also remove the static members which allow us to code to interfaces as well as easily create mocked instances which lead to greater malleability of our code as well as improved testability.  You can download the sample code from here.

kick it on DotNetKicks.com

Feedback

# re: The Monostate Pattern

The three points about singletons:

1) They break OOP (no inheritance)
-- Singletons should not be overridden anyhow. This, in and of itself isn't bad. Otherwise the keyword "sealed" is also bad OO design.

2) Singletons introduce dependencies between classes
-- True, but only if you program that way. Singletons can implement interfaces, which allows you to decouple classes and use dependency injection. A factory can return a singleton as easily as any other object (as long as it knows about singletons, of course).

3) They are static and cannot be mocked (easily)
-- Again, interfaces. Also, singleton != static. The only static part of a singleton in .NET should be the Instance property.

Dissing aside, this is a nice pattern. My only problem with it is that it might not be immediately apparent to callers that this is actually a singleton in disguise, which may cause some maintenance problems.

I don't see how this is all that different from a singleton, if you construct the singleton so that it implements an interface. If I was faced with having to use a mock library that couldn't handle singletons this pattern would be perfect. 4/21/2008 2:18 PM | Will

 re: The Monostate Pattern

I'm with Will. I would have implemented your MenuController class as so:

public class MenuController: IMenuController
{
private static IMenuController _instance;
private int _activeMenuIndex;

private MenuController(){}

public IMenuController Instance {
get {
return _instance ?? new MenuController();
}
}

public int ActiveMenuIndex {
get { return _activeMenuIndex; }
}

public int GetValueOfCurrentMenu() {
// fake value
return 10;
}
}


This class is still mockable as it implements the proper interface, only allows one instance of itself, and most certainly can be overridden, or inherited from. Castle Windsor can use the factory facility to instantiate this object as well. I'm sure other IoC containers can do the same, but Windsor is what I'm most familiar with.

I can certainly see uses for wrapping an implementation in a proxy like you have (no need for a factory). Thanks for putting this out there, I've never heard of the Monostate Pattern before, and it's cool to see another way of solving this problem. 4/21/2008 4:02 PM | Bryan

 re: The Monostate Pattern

I botched my code above..

My code would return a new instance everytime, not the same single instance.

public IMenuController Instance {
get {
if(_instance == null)
_instance = new MenuController();
return _instance;
}
}
4/21/2008 4:04 PM | Bryan

# re: The Monostate Pattern

Thanks for the feedback guys. I actually had never considered simply having the singleton class implement the required interface. Sometimes the simplest solution really is the best.

One advantage I do really see with the monostate, however, is the fact that the implementation details are hidden from the client. As long as client code is simply "newing" up the required class, I have the freedom to decide that I no longer want it to be a singleton and not break any existing client code.

Jeremy 4/22/2008 8:35 AM | Jeremy

# re: The Monostate Pattern

I have similar negative views on singletons. Fortunately they can usually be designed out easily, so they don't come up much anymore. http://www.prestonlee.com/archives/22 7/4/2008 2:41 PM | Preston Lee

# re: The Monostate Pattern

@Preston,

Thanks for the comment, by the way, I love your linked post. The phrase about "terrorist patterns" is hilarious, good content, too.

Add to my OPML...
jeremy 7/5/2008 7:36 PM | Jeremy

Post a comment





 

Please add 5 and 6 and type the answer here: