Skip to content

Instantly share code, notes, and snippets.

@yangruihan
Last active February 15, 2019 08:09
Show Gist options
  • Select an option

  • Save yangruihan/49176266c045e2510fcb0f28f0049975 to your computer and use it in GitHub Desktop.

Select an option

Save yangruihan/49176266c045e2510fcb0f28f0049975 to your computer and use it in GitHub Desktop.
单例抽象类
public abstract class Singleton<T> where T : class, new()
{
private static readonly object LockObject = new object();
private static T _instance;
public static T Instance
{
get
{
if (_instance != null) return _instance;
lock (LockObject)
{
if (_instance == null)
_instance = new T();
}
return _instance;
}
}
public void Free()
{
_instance = null;
FreeSingleton();
}
protected virtual void FreeSingleton()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment