C#的单例模式 以及 消息分发机制实现

C#单例模式,多线程安全

1//Singleton.cs 2public class Singleton<T> where T : new() 3{ 4    private static T _instance; 5    private static object _lock = new object(); 6 7    protected Singleton() {} 8    public static T Instance() 9    { 10        if(_instance == null) 11        { 12            lock(_lock) 13            { 14                if(_instance == null) 15                { 16                    _instance = new T(); 17                } 18            } 19        } 20        return _instance; 21    } 22}

C#消息分发机制,用了delegate

1//MessageDispatcher.cs 2using System.Collections; 3using System.Collections.Generic; 4public class MyMessageDispatcher : Singleton<MyMessageDispatcher> 5{ 6    Dictionary<uint, MessageHandler> m_HandlerMap; 7     8    public MyMessageDispatcher() 9    { 10        m_HandlerMap = new Dictionary<uint, MessageHandler> (); 11    } 12     13    public void RegisterMessageHandler(uint iMessageType, MessageHandler handler) 14    { 15        if (handler == null) 16            return; 17         18         19        if (!m_HandlerMap.ContainsKey (iMessageType))  20        { 21            MessageHandler Handlers = handler; 22            m_HandlerMap.Add (iMessageType, Handlers); 23        }  24        else  25        { 26            m_HandlerMap[iMessageType] += handler; 27        } 28    } 29     30    public void UnRegisterMessageHandler(uint iMessageType,  MessageHandler handler) 31    { 32        if (handler == null) 33            return; 34         35        if(m_HandlerMap.ContainsKey (iMessageType)) 36        { 37            m_HandlerMap[iMessageType] -= handler; 38        } 39    } 40     41    public void SendMessage(uint iMessageType, object arg) 42    { 43        if(m_HandlerMap.ContainsKey (iMessageType)) 44        { 45            if(m_HandlerMap[iMessageType] != null) 46            { 47                m_HandlerMap[iMessageType].Invoke(iMessageType, arg); 48            } 49        } 50    } 51} 52 53//EMessageType.cs中定义事件类型 54public enum EUIMessage : uint 55{ 56    UITest0 = 0, 57    UITest1 = 1, 58    UITest2 = 2 59}

使用消息分发机制示例:

1//发出消息事件 2MessageDispatcher.Instance().SendMessage((uint)EUIMessage.UITest0, “ouput”); 3 4//接受消息端,先要注册侦听消息,此处的例子是在Unity3D中实现的 5public class TestText : MonoBehaviour  6{ 7    void OnEnable() 8    { 9        MyMessageDispatcher.Instance ().RegisterMessageHandler ((uint)EUIMessage.UITest0, ChangePlay0); 10    } 11 12    void OnDisable() 13    { 14        MyMessageDispatcher.Instance ().UnRegisterMessageHandler ((uint)EUIMessage.UITest0, ChangePlay0); 15    } 16 17    public void ChangePlay0(uint iMessageType, object kParam) 18    { 19        string txt = (string) kParam; 20        GUIText text = GetComponent<GUIText>(); 21        text.text = txt ; 22    } 23}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Mybatis Plus代码方式实现多表关联查询

 Mapper接口如下:例1@Select("SELECTt\_question.\,t\_student.\name\FROMt\_question,t\_studentWHEREt\_question.student\_idt\_student.id")List<QuestionStudentVOgetQ

C#的单例模式 以及 消息分发机制实现 - HelloWorld