设计模式——单例模式

一:介绍

定义:确保一个类只有一个实例,并提供一个全局访问点
单例模式是最简单的设计模式之一,属于创建型模式。计算机中的任务管理器,回收站是单例的典型应用场景
注意:
1、单例类只能有一个实例
2、单例类必须自己创建自己的唯一实例
3、单例类必须给所有其他对象提供这一实例


二:几种单例模式的写法

——饿汉式:在调用Instance对象之前就实例化对象

using System;
using UnityEngine;

public class Singleton : MonoBehaviour
{
    //继承MonoBehaviour
    private static Singleton _instance;
    public static Singleton Instance
    {
        get { return _instance; }
    }

    private void Awake()
    {
        _instance = this;
    }

//    //不继承MonoBehaviour
//    private static Singleton _instance = new Singleton();
//    public static Singleton Instance
//    {
//        get { return _instance; }
//    }
}


——懒汉式:在调用Instance对象之后才实例化对象

using System;
using UnityEngine;

public class Singleton : MonoBehaviour
{
    //继承MonoBehaviour
    private static Singleton _instance;
    public static Singleton Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<Singleton>();
            }
            return _instance;
        }
    }

//    //不继承MonoBehaviour
//    private static Singleton _instance;
//    public static Singleton Instance
//    {
//        get
//        {
//            if (_instance == null)
//            {
//                _instance = new Singleton();
//            }
//            return _instance;
//        }
//    }
}

三:单例模版

——单例类模板(继承MonoBehaviour)

using UnityEngine;

public abstract class MonoSingleton<T> : MonoBehaviour
    where T : MonoBehaviour
{
    private static T m_Ins = null;
    public static T Ins
    {
        get
        {
            if (m_Ins == null)
            {
                m_Ins = FindObjectOfType(typeof(T)) as T;
                if (m_Ins == null)
                {
                    m_Ins = new GameObject(typeof(T).ToString(), typeof(T)).GetComponent<T>();
                    var root = GameObject.Find("MonoSingleton");
                    if (root == null)
                    {
                        root = new GameObject("MonoSingleton");
                        DontDestroyOnLoad(root);
                    }
                    m_Ins.transform.parent = root.transform;
                }
            }
            return m_Ins;
        }
    }
}


 


——单例类模板(不继承MonoBehaviour)

public abstract class Singleton<T>
    where T : class, new()
{
    static T m_Ins;
    public static  T Ins
    {
        get
        {
            if (m_Ins == null)
            {
                m_Ins = new T();
            }
            return m_Ins;
        }
    }

    protected Singleton()
    {
        Init();
    }

    public virtual void Init()
    {

    }

    public static void Destroy()
    {
        m_Ins = null;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello Bug.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值