Unity教程合集程序员征服Unity3d

【设计模式】单例模式浅析

2016-11-08  本文已影响0人  黒可乐

引子

在学习编程之初我们就知道使用静态方法或者是静态变量就可以在其他类中使用那个方法那个变量,但是老师就告诉我们能不用静态方法就不用,因为静态的不能继承巴拉巴拉...就一直很怕使用静态,直到单例的出现。感觉人生一下子就豁然开朗。单例与静态的区别 看完了这个总有一种误会静态的感受。哈哈哈。


既然是单例,就意味着就只能有一个对象,所以他的构造函数肯定是私有的。

public class MyClass
{
  private MyClass()
  {
  }
}

简而言之就是这样了,这个是我在平时写的时候经常忘记写的一个,当然不写也不会报错,个人觉得写上还是严谨一些嘛,哈哈。当然继承于MonoBehriour的类是不建议new的嘛,new的话unity也会给你报警告的。当然如果还是要在实例化对象的话,就可以不加这个私有构造方法。就给大家说说我都用过那几种写法嘛。下面的例子就不写这个方法。


最简单Awake

public class MyClass : MonoBehaviour
{
    public static MyClass instance;
    public void Awake()
    {
        instance = this;
    }
}

这样的写法是最简单的,也是最粗暴的。当然一般这样的写法按照常规剧本走的话,到后面都不会有好的结果。用了这个的话在多线线程中是不能好好跟你玩耍的。其实在做一些小demo还是可以这个滴,毕竟简单嘛。

不继承于MonoBehavior常规类

public class MyClass
{
    public static MyClass Instance
    {
        get { return Nest.instance; }
    }

    private class Nest
    {
        static Nest()
        {
        }
        internal static readonly MyClass instance = new MyClass();
    }
}

这种是静态内部类,既是线程安全又没有消耗太多。

继承于MonoBehavior

using UnityEngine;

public class MyClass : MonoBehaviour
{
    private static MyClass instance;

    public static MyClass Instance
    {
        get
        {
            if (!instance)
            {
                instance = (MyClass) FindObjectOfType(typeof (MyClass));
                if (!instance)
                {
                    Debug.LogError("There needs to be one active MyClass script on a GameObject in your scene");
                }
            }
            return instance;
        }
    }
}

这个写法是一定要这个脚本挂在游戏物体上的。


平时写代码时我们不可能在每一个要使用的单例模式的类中都写上上面的代码,太麻烦了,下面给大家介绍两种之前遇到的两种简便方法。


单例模板基类

using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : class
{
    protected static T instance;
    public static T Instance
    {
        get
       {
            if (null == instance)
            {
                var type = typeof (T);
                if (type.IsAbstract || type.IsInterface)
                {
                    throw (new Exception("Class type must could be instantiated. Don't use abstract or interface"));
                }
                if (!type.IsSealed)
                {
                    throw (new Exception("Class type must be a sealed. Use sealed"));
                }
                var constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    SType.EmptyTypes,
                    null);
                if (null == constructor)
                {
                    throw (new Exception("Constructor must empty"));
                }
                if (!constructor.IsPrivate)
                {
                    throw (new Exception("Constructor must be a private function"));
                }
                instance = constructor.Invoke(null) as T;
            }
            return instance;
        }
    }
}

这个模板可以继承于monobehaviour也可以不用,继承于这个基类的类是不能有派生的

变量型单例

using UnityEngine;
public class InstanceManager : MonoBehaviour
{
   private static InstanceManager instance;

   public static InstanceManager Instance
  {
      get
      {
          if (!instance)
          {
              instance = (InstanceManager) FindObjectOfType(typeof (InstanceManager));
              if (!instance)
              {
                  Debug.LogError("There needs to be one active MyClass script on a GameObject in your scene");
              }
          }
          return instance;
      }
  }

  #region add public script
        public start Start;
  #endregion
}

细心的你肯定发现了这样的写法与上面继承于monobehaviour的方式是一样的,后面加了一个公共的类的变量,这个类就是咱们需要使用的的单例类。当然这个管理单例脚本也要挂在游戏物体上,只需要在外面给其赋值。(当然也可以动态赋值,虽然没有动态赋值过,不过可以试试)


直接赋值

综合看起来个人觉得第一种模板基类跟适合用来不用放在场景中一些管理器,第二种就适合在场景用到的一些单例脚本。

上一篇 下一篇

猜你喜欢

热点阅读