I have a number of classes doing different things but using the same Singleton pattern from http://www.yoda.arachsys.com/csharp/singleton.html
public sealed class Singleton
{
static Singleton instance=null;
static readonly object padlock = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
}
Does anyone have a neat way I can reuse as much of the common Singleton code as possible between the different classes?
For example if I have SingletonJob1 and SingletonJob2 I'd like to be able to change the code in only place if I move to one of the other Singleton patterns.
Edit: Yes as people have pointed out Method 5 from http://www.yoda.arachsys.com/csharp/singleton.html is less code. I did read to the end of the page! I chose method 2 because the Singleton objects relate to hardware devices and I want only want a couple of them initialised and used in any given run of the program. Method 5 will initialise them all straight away.
Is there any reason you're using that version rather than the simpler one which just initializes the instance in the declaration?
public class Singleton
{
private static Singleton instance = new Singleton();
public static Singleton Instance { get { return instance; } }
// Only use this if you really need it - see the page for details
static Singleton() {}
private Singleton()
{
// I assume this logic varies
}
}
This pattern is sufficiently short that I don't think it's much of a problem to include it everywhere.
I would urge you to consider whether you really need that many singletons; they're generally not great for testability etc.
EDIT: If you really, really want laziness and you're using .NET 4, you can get it with the 6th pattern which is on the singleton page's new home:
public sealed class Singleton
{
private static readonly Lazy<Singleton> lazy =
new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
private Singleton()
{
}
}
public static class Singleton<T> where T: new()
{
static T instance=null;
static readonly object padlock = new object();
public static T Instance
{
get
{
lock (padlock)
{
if (instance==null)
{
instance = new T();
}
return instance;
}
}
}
}
So you can use your Singleton for all Classes:
Singleton<YourClass>.Instance.YourMethod();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With