单例模式

1.概述

单例模式(Singleton Pattern):确保一个类只有一个实例,并提供一个全局访问点来访问这个唯一的实例。

某个类只有一个实例,还得自行创建这个实例,还得自行向整个系统提供这个实例。

2.结构

Singleton(单例类):私有构造函数,静态私有成员变量,静态共有工厂方法。

3.实现

using UnityEngine;

namespace Singleton
{
    public class Singleton
    {
        private static Singleton singleton = null;
        private Singleton() { }
        public static Singleton GetSingleton()
        {
            if (singleton == null)
            {
                singleton = new Singleton();
            }
            return singleton;
        }

        private string name;
        public string GetName()
        {
            return name;
        }
    }
    public class Client
    {
        public void UseMethod()
        {
            string name = Singleton.GetSingleton().GetName();
        }
    }

    //-- Eager Singleton 饿汉式单例类
    public class EagerSingleton
    {
        private static EagerSingleton singleton = new EagerSingleton();
        private EagerSingleton() { }
        public static EagerSingleton GetSingleton()
        {
            return singleton;
        }
    }
    //-- Lazy Singleton 懒汉式单例类
    public class LazySingleton
    {
        private static LazySingleton singleton = null;
        private static readonly object syncRoot = new object();
        private LazySingleton() { }
        public static LazySingleton GetSingleton()
        {
            if (singleton == null)
            {
                lock (syncRoot)
                {
                    if (singleton == null)
                    {
                        singleton = new LazySingleton();
                    }
                }
            }
            return singleton;
        }
    }
    //--ManualSingleton
    public abstract class ManualSingleton<T> where T : class
    {
        private static T instance = null;
        public static void Init()
        {
            System.Type type = typeof(T);
            System.Reflection.ConstructorInfo[] constructorInfos = type.GetConstructors(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            foreach (System.Reflection.ConstructorInfo constructorInfo in constructorInfos)
            {
                System.Reflection.ParameterInfo[] parameterInfos = constructorInfo.GetParameters();
                if (parameterInfos.Length == 0)
                {
                    instance = (T)constructorInfo.Invoke(null);
                    break;
                }
            }
        }
        public static T Instance
        {
            get { return instance; }
        }
        public static void Release()
        {
            instance = null;
        }
    }
    //--Nested
    public class MySingleton
    {
        private MySingleton() { }
        private static class Nested
        {
            internal static readonly MySingleton singleton = new MySingleton();
        }
        public static MySingleton Instance
        {
            get { return Nested.singleton; }
        }
    }
    public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        private static T m_Instance = null;
        public static T Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = GameObject.FindObjectOfType(typeof(T)) as T;
                    if (m_Instance == null)
                    {
                        m_Instance = new GameObject("Singleton of " + typeof(T).ToString(), typeof(T)).GetComponent<T>();
                    }
                }
                return m_Instance;
            }
        }
        private void Awake()
        {
            if (m_Instance == null)
            {
                m_Instance = this as T;
            }
        }
        private void OnApplicationQuit()
        {
            m_Instance = null;
        }
    }
}

4.优缺点

(1)单例模式提供唯一实例的受控访问,可严格控制用户怎样访问它以及何时访问它。

(2)系统中只存在一个对象,提高系统性能。

(3)单例模式允许可变数目的实例。

(1)没有抽象层,导致扩展困难。

(2)职责过重,既提供业务方法又提供工厂方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值