当前位置: 首页> 科技> 名企 > 品牌市场营销策略_门户网站与搜索引擎的区别_品牌推广文案_推广免费

品牌市场营销策略_门户网站与搜索引擎的区别_品牌推广文案_推广免费

时间:2025/7/11 15:05:27来源:https://blog.csdn.net/2301_76618920/article/details/142865897 浏览次数:0次
品牌市场营销策略_门户网站与搜索引擎的区别_品牌推广文案_推广免费

1. 为什么要写写单例模式基类

用面向对象的思想避免代码冗余(多余、重复)

2. 实现不继承MonoBehaviour的单例模式基类

单例模式的基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 单例模式基类,主要目的是避免代码的冗余,方便实现单例模式的类
/// </summary>
/// <typeparam name="T"></typeparam>
//where约束T必须是class,还有有一个公共的无参构造函数
public class BaseManager<T> where T : class,new()
{public static T instance;// 属性的方式public static T Instance{get{if(instance == null){instance = new T();}return instance;}}// 方法的方式public static T GetInstance(){if(instance == null){instance = new T();}return instance;}
}

当其他类需要实现单例时候,直接继承基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TestMgr : BaseManager<TestMgr>
{public void TestLog(){Debug.Log("Test");}}

好处就是可以避免代码冗余,不用重复定义单例,需要单例直接继承就行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TestMgr2 : BaseManager<TestMgr2>
{public void TestLog(){Debug.Log("test2");}}

测试

public class Main : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){TestMgr.Instance.TestLog();TestMgr2.Instance.TestLog();}// Update is called once per framevoid Update(){}
}

在这里插入图片描述

3. 潜在的安全问题

1.构造函数问题:构造函数可在外部调用 可能会破坏唯一性
比如csharp TestMgr t = new TestMgr(); 不能new一个单例模式的对象,可以通过反射解决
2.多线程问题:当多个线程同时访问管理器时,可能会出现共享资源的安全访问问题

关键字:品牌市场营销策略_门户网站与搜索引擎的区别_品牌推广文案_推广免费

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: