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?