Unity 单例类写法

单例是设计模式中最简单也是最好用的一种,通常一个项目中可能有很多类要写成单例模式,所以可以写一个单例类,然后通过继承它。

单例类写法:

public class Singleton where T : new()
{
    static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new T();
            }
            return instance;
        }
        
    }
}

你可能感兴趣的:(Unity学习记录,单例模式,Java,泛型,设计模式,代码复用)