[游戏开发常用设计模式]单例模式

概念: 

单例模式是一种创建型设计模式,它确保一个类只有一个实例,并且提供一个全局访问点来访问改实例。

  • 饿汉单例模式

    这种单例模式的实现方式是在类加载时立即初始化单例对象。在类被加载时,单例对象就已经被创建,因此线程安全,但如果单例对象非常复杂且不常用,可能会影响程序启动时间和内存占用。

public class EagerSingleton
{
    private static readonly EagerSingleton instance = new EagerSingleton();
    
    static EagerSingleton() {}
    
    private EagerSingleton() {}
    
    public static EagerSingleton Instance
    {
        get { return instance; }
    }
}

  • 懒汉单例模式

    这种单例模式的实现方式是在第一次调用获取单例对象的方法时创建单例对象。这种方式在访问单例对象之前不会创建单例对象,因此可以节省内存空间,但是需要考虑线程安全的问题。

public class LazySingleton
{
    private static LazySingleton instance;
    
    private LazySingleton() {}
    
    public static LazySingleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new LazySingleton();
            }
            return instance;
        }
    }
}

  • 双重检查锁定单例模式

    这种单例模式的实现方式是在懒汉式单例模式的基础上增加了双重检查锁定机制,以确保线程安全。该模式首先检查单例对象是否已经创建,如果没有创建,则加锁创建单例对象,再次检查单例对象是否已经创建。

public class DoubleCheckedSingleton
{
    private static readonly object lockObj = new object();
    private static DoubleCheckedSingleton instance;
    
    private DoubleCheckedSingleton() {}
    
    public static DoubleCheckedSingleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (lockObj)
                {
                    if (instance == null)
                    {
                        instance = new DoubleCheckedSingleton();
                    }
                }
            }
            return instance;
        }
    }
}

  • 静态内部类单例模式

    这种单例模式的实现方式是使用静态内部类来实现单例。该模式在类被加载时不会立即初始化单例对象,而是在调用获取单例对象的方法时才会初始化单例对象,因此可以避免线程安全的问题。

public class StaticNestedSingleton
{
    private StaticNestedSingleton() {}
    
    private class NestedSingleton
    {
        internal static readonly StaticNestedSingleton instance = new StaticNestedSingleton();
    }
    
    public static StaticNestedSingleton Instance
    {
        get { return NestedSingleton.instance; }
    }
}

单例模式在Unity中的实现方式:

一,饿汉模式

public class EagerSingleton : MonoBehaviour
{
    private static readonly EagerSingleton instance = new EagerSingleton();
    
    static EagerSingleton() {}
    
    private EagerSingleton() {}
    
    public static EagerSingleton Instance
    {
        get { return instance; }
    }
    
    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
    }
}

二,懒汉模式

public class LazySingleton : MonoBehaviour
{
    private static LazySingleton instance;
    
    private LazySingleton() {}
    
    public static LazySingleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new GameObject("LazySingleton").AddComponent<LazySingleton>();
                DontDestroyOnLoad(instance.gameObject);
            }
            return instance;
        }
    }
}

三,双重检查锁定单例模式

public class DoubleCheckedSingleton : MonoBehaviour
{
    private static readonly object lockObj = new object();
    private static DoubleCheckedSingleton instance;
    
    private DoubleCheckedSingleton() {}
    
    public static DoubleCheckedSingleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (lockObj)
                {
                    if (instance == null)
                    {
                        instance = new GameObject("DoubleCheckedSingleton").AddComponent<DoubleCheckedSingleton>();
                        DontDestroyOnLoad(instance.gameObject);
                    }
                }
            }
            return instance;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/lel18570471704/article/details/126071520