We have some legacy code that has a bunch of singletons all over the place (written in C#).
The singleton is a fairly “classic” implementation of the pattern:
public class SomeSingleton
{
private static SomeSingleton instance;
private SomeSingleton()
{
}
public static SomeSingleton Instance
{
get
{
if (instance == null)
{
instance = new SomeSingleton();
}
return instance;
}
}
}
Note that thread safety is not a concern, so no locks are used.
In order to make the code more testable, and without making too many modifications, I’d like to modify this code to delegate the creation of the singleton instance in another class (a factory or similar pattern).
This can assist in creating a “test” instance for testing purposes, or the real version, as it is used now.
Is this a common practice? I could not find any reference to such pattern being used.
7
In the comments you say you are using Unity and Mono for game development. I am guessing that means Unity3D not Microsoft Unity. As such I would recommend that you ditch the singleton pattern that you are following and instead use dependency injection. I believe you can use Zenject with the unity3d framework.
The singleton class would be modified to be an instance of an interface like:
public class IImportantInterface
{
DoSomethingImportant();
}
public class MySingletonImplementation: IImportantInterface
{
public void DoSomethingImportant()
{
Console.WriteLine("This is Important!");
}
}
public class DoImportantStuff
{
private readonly IImportantInterface _ImportantInterface;
public DoImportantStuff(IImportantInterface importantInterface )
{
_importantInterface = importantInterface;
}
public void DoSomething()
{
_importantInterface.DoSomethingImportant();
}
}
Then in you startup logic you can register a singleton instance with Zenject:
Container.Bind<IImportantInterface>().ToSingle<MySingletonImplementation>();
A good blog post about this concept can be found here:
http://www.unityninjas.com/code-architecture/dependency-injection/
This calls for inversion of control. Just start by adding an interface to SomeSingleton say ISomeSingleton. Then all consumers of ISomeSingleton will just get an constructor parameter of ISomeSingleton.
Example:
public class Consumer1 {
readonly ISomeSingleton _mySingleton;
public Consumer1(ISomeSingleton mySingleton) {_mySingleton = mySingleton;}
}
public interface ISomeSingleton{//All your methods you ned.}
public class SomeSingleton : ISomeSingleton
{
private static SomeSingleton instance;
public SomeSingleton() // constructor is now public
{
}
public static SomeSingleton Instance // todo remove this when all consumers follow inversino of control pattern.
{
get
{
if (instance == null)
{
instance = new SomeSingleton();
}
return instance;
}
}
}
When no consumer calls the SomeSingleton directly you can just remove it. This allows you to make the change at your own pace.
Now you just need to figure out how to instantiate SomeSingleton. You can just do this manually or you can start using an IOC container of you feel like it.
In testing you can just create a different implementation of ISomeSingleton.
3