美文网首页
两个单例模板

两个单例模板

作者: 君子藏器於身 | 来源:发表于2017-04-13 10:56 被阅读0次

单例在Unity是老生常谈的一个设计模式,并不是它有多么伟大,而是它有多么的方便,当然方便不等于滥用,我们要区分单例用的场合。在我的设计中,大多用来作为管理类,以下两个模板皆来自前辈们的手笔,部分为自己修改。

1.普通类的单例模板

public ClassSigleton<T> where T :Class
{
        private static T _instance;
        /// <summary>
        /// 添加一个锁,防止多线程造成的多个实例,并加入私有构造函数
        /// </summary>
        private static readonly object syncRoot = new object();

        public static readonly Type[] EmptyTypes = new Type[0];
         public static T instance
        { 
           get
           {
               if (_instance == null)
               {
                   lock (syncRoot)
                   {
                       if (_instance == null)
                       {
                           ConstructorInfo ci = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, EmptyTypes, null);
                           if (ci == null)
                           {
                               throw new InvalidOperationException("class must contain a private constructor");
                           }
                           _instance = (T)ci.Invoke(null);
                       }
                   }
               }
               return _instance;
           }
        }
}

2.基于MonoBehaviour

  /// <summary>
 /// MonoBehaviour单利模板类
 /// 一定不要在Destroy函数中直接访问单例模式,非要使用实现判断instance是否存在
    /// </summary>
 public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
 {
       private static T _instance;
       public Transform cacheTransform
       {
           get;
           protected set;
       }

       public GameObject cacheGameObject
       {
           get;
           protected set;
       }
       /// <summary>
       /// 添加一个锁,防止多线程造成的多个实例,并加入私有构造函数
       /// </summary>
       private static readonly object syncRoot = new object();
       public static T instance
       {
           get
           {
               if (_instance == null)
               {
                   lock (syncRoot)
                   {
                       if (_instance == null)
                       {
                           _instance = GameObject.FindObjectOfType(typeof(T)) as T;
                           if (_instance == null)
                           {
                               GameObject coreGameObject = new GameObject(typeof(T).Name);
                               _instance = coreGameObject.AddComponent<T>();
                               DontDestroyOnLoad(coreGameObject);
                           }
                       }
                   }
               }
               return _instance;
           }
       }

       protected virtual void Awake()
       {
           cacheTransform = transform;
           cacheGameObject = gameObject;
       }

       protected virtual void Destroy()
       {
           _instance = null;
       }
 }

相关文章

  • 两个单例模板

    单例在Unity是老生常谈的一个设计模式,并不是它有多么伟大,而是它有多么的方便,当然方便不等于滥用,我们要区分单...

  • 模板

    使用场景:设计单例类的时候,可以设计通用的单例类,使用模板。 KCBP中使用模板的地方:

  • 单例模板

    为避免来反复写单例模式,在框架中构建一个单例模板,需要用的地方直接继承就可。 基本要求 单例模式必须继承自Mono...

  • C++单例模板

    单例模板类: template classSingleton { public: Singleto...

  • python单例模式

    python单例模式实现方式 使用模板 python模块是天然的单例模式(.pyc文件的存在) 使用__new__...

  • monobehavier单例模板

    using UnityEngine; using System.Collections; public class...

  • OC底层知识点之-多线程(四)GCD下篇

    单例 说起单例,我们一般使用GCD的dispath_once来创建单例 对于单例,需要知道以下两个问题: 1.单例...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • 自己整理的一些关于Spring源码的总结,面试之前回来看看(不定

    Spring中的设计模式? 1.工厂2.单例(看看内部枚举实现的单例)3.委派(delegate)4.模板方法(r...

  • 设计模式

    单例模式 模板方法模式 工厂模式 代理模式 静态代理 JDK动态代理

网友评论

      本文标题:两个单例模板

      本文链接:https://www.haomeiwen.com/subject/vuqbattx.html