栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

设计模式——单例模式

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设计模式——单例模式

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

文章目录
  • 前言
  • 一、单例模式
  • 二、实现
  • 二、用途
  • 三、优缺点
  • 总结


前言

单例模式学习总结.下面代码中继承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 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 where T : new()
    {
        private static T s_singleton = default(T);
        private static object s_objectLock = new object();
        public static T singleton
        {
            get
            {
                if (Singleton.s_singleton == null)
                {
                    object obj;
                    Monitor.Enter(obj = Singleton.s_objectLock);//加锁防止多线程创建单例
                    try
                    {
                        if (Singleton.s_singleton == null)
                        {
                            Singleton.s_singleton = ((default(T) == null) ? Activator.CreateInstance() : default(T));//创建单例的实例
                        }
                    }
                    finally
                    {
                        Monitor.Exit(obj);
                    }
                }
                return Singleton.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 : MonoBehaviour where T : MonoBehaviour
{
    public bool global = true;
    static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance =(T)FindObjectOfType();
            }
            return instance;
        }


    }


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


    protected virtual void OnStart()
    {


    }
}

二、用途

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


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

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


总结

保持饥饿,保持愚蠢.

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/902071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号