Last active
February 15, 2019 08:09
-
-
Save yangruihan/49176266c045e2510fcb0f28f0049975 to your computer and use it in GitHub Desktop.
单例抽象类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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