Factory for creating a singleton instance

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật