单例在我们开发过程使用的次数有许多,在一次项目中都需要写很多次,因为为了节省就计划写一个模板,只需要继承即可
using System;
using System.Collections.Generic;
using System.Linq;
public class Singleton<T> where T : new()
{
private static T ms_instance;
public static T Instance
{
get
{
if (ms_instance == null)
{
ms_instance = new T();
}
return ms_instance;
}
}
}
因为我使用的C#做公司项目,因此暂时实现C#的模板。其他语言其实都是类似的。
最后说一句:不喜欢勿喷!!!