设计模式——单例模式

声明:本文为个人笔记,用于学习研究使用非商用,内容为个人研究及综合整理所得,若有违规,请联系,违规必改。


前言

单例模式学习总结.下面代码中继承MonoBehaviour为Unity的脚本.非Unity开发无须在意.


一、单例模式

定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点


二、实现

  • 常规单例
public class UIManager
  {
    //单例模式
    private static UIManager instance;
    public static UIManager Instance
    {
      get {
        if (instance == null)
        {
          instance = new UIManager();
        }
        return instance;
    }
  • 泛型单例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


public class Singleton<T> where T : new()
{
    static T instance;


    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new T();
            }
            return instance;
        }
        
    }
}

  • 加锁的普通单例
  class Singleton
    {
        private static Singleton instance;
        private static readonly object syncRoot = new object();
        private Singleton()
        {
        }

        public static Singleton GetInstance()
        {
            if (instance == null)
            {

                lock (syncRoot)
                {

                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }

    }
  • 加锁的泛型单例
public class Singleton<T> where T : new()
    {
        private static T s_singleton = default(T);
        private static object s_objectLock = new object();
        public static T singleton
        {
            get
            {
                if (Singleton<T>.s_singleton == null)
                {
                    object obj;
                    Monitor.Enter(obj = Singleton<T>.s_objectLock);//加锁防止多线程创建单例
                    try
                    {
                        if (Singleton<T>.s_singleton == null)
                        {
                            Singleton<T>.s_singleton = ((default(T) == null) ? Activator.CreateInstance<T>() : default(T));//创建单例的实例
                        }
                    }
                    finally
                    {
                        Monitor.Exit(obj);
                    }
                }
                return Singleton<T>.s_singleton;
            }
        }
        protected Singleton()
        {
        }
  • 继承MONO的普通单例
sing UnityEngine;
using System.Collections;

public class test2 : MonoBehaviour {
    public static test2 instance;
    // Use this for initialization
    void Awake () {
        instance = this;
    }
}
  • 继承Mono的抽象单例
using UnityEngine;

public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
    public bool global = true;
    static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance =(T)FindObjectOfType<T>();
            }
            return instance;
        }


    }


    void Start()
    {
        if (global) DontDestroyOnLoad(this.gameObject);
        this.OnStart();
    }


    protected virtual void OnStart()
    {


    }
}

三、用途

让类可以像静态类一样被全局访问,且保证只有一个实例.自己项目中常用于Manager及Service等管理及服务类中.


四、优缺点

  • 优点:使用方便,灵活.

  • 缺点: 除非被销毁了,不然实例创建后一直存在,无论是否使用.


总结

保持饥饿,保持愚蠢.

这世界唯一能够相信的就是你付出的努力和你走过的路.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ۓ明哲ڪ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值