观察者 (事件委托)
来源:互联网 发布:运营数据分析 数据挖掘 编辑:程序博客网 时间:2024/06/05 11:04
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//委托事件
public class Notifications
{
private static Notifications _event;
public static Notifications Event
{
get
{
return _event ?? (_event = new Notifications());
}
}
public class Args : System.EventArgs { }
private static Dictionary<string, List<Pair>> eventsDic = new Dictionary<string, List<Pair>>();//事件集合
public delegate void StandardDelegate(Args e);
class Pair
{
public object obj;
public StandardDelegate deleget;
public Pair(object obj, StandardDelegate d)
{
this.obj = obj;
this.deleget = d;
}
}
/// <summary>
/// 注册事件
/// </summary>
/// <param name="eventName">事件名</param>
/// <param name="obj"></param>
/// <param name="d">时间委托</param>
public void Register(string eventName, object obj, StandardDelegate d)
{
//判断是否存在该事件
if (eventsDic.ContainsKey(eventName))
{
foreach (Pair pair in eventsDic[eventName])
{
if (pair.Equals(obj))
{
pair.deleget += d;
return;
}
}
eventsDic[eventName].Add(new Pair(obj, d));
}
else
{
List<Pair> pList = new List<Pair>();
pList.Add(new Pair(obj, d));
eventsDic.Add(eventName, pList);
}
}
/// <summary>
/// 取消事件
/// </summary>
/// <param name="obj"></param>
public void DeRegister(object obj)
{
List<string> funcNameList = new List<string>();
foreach (KeyValuePair<string, List<Pair>> kv in eventsDic)
{
var values = kv.Value;
for (int i = 0; i < values.Count; i++)
{
if (values[i].obj.Equals(obj))
{
values.RemoveAt(i);
continue;
}
if (values.Count == 0)
{
funcNameList.Add(kv.Key);
}
}
//从字典中移除List<Pair>为空的键值
foreach (string s in funcNameList)
{
if (eventsDic.ContainsKey(s))
{
eventsDic.Remove(s);
}
}
}
}
/// <summary>
/// 调用时间
/// </summary>
/// <param name="eventName"></param>
/// <param name="e"></param>
public void Fire(string eventName, Args e)
{
if (eventsDic.ContainsKey(eventName))
{
foreach (Pair pair in eventsDic[eventName])
{
if (pair.obj != null && pair.deleget != null)
{
pair.deleget(e);
}
}
}
else
{
Debug.LogError("没有注册过该事件");
}
}
}
using System.Collections;
using System.Collections.Generic;
//委托事件
public class Notifications
{
private static Notifications _event;
public static Notifications Event
{
get
{
return _event ?? (_event = new Notifications());
}
}
public class Args : System.EventArgs { }
private static Dictionary<string, List<Pair>> eventsDic = new Dictionary<string, List<Pair>>();//事件集合
public delegate void StandardDelegate(Args e);
class Pair
{
public object obj;
public StandardDelegate deleget;
public Pair(object obj, StandardDelegate d)
{
this.obj = obj;
this.deleget = d;
}
}
/// <summary>
/// 注册事件
/// </summary>
/// <param name="eventName">事件名</param>
/// <param name="obj"></param>
/// <param name="d">时间委托</param>
public void Register(string eventName, object obj, StandardDelegate d)
{
//判断是否存在该事件
if (eventsDic.ContainsKey(eventName))
{
foreach (Pair pair in eventsDic[eventName])
{
if (pair.Equals(obj))
{
pair.deleget += d;
return;
}
}
eventsDic[eventName].Add(new Pair(obj, d));
}
else
{
List<Pair> pList = new List<Pair>();
pList.Add(new Pair(obj, d));
eventsDic.Add(eventName, pList);
}
}
/// <summary>
/// 取消事件
/// </summary>
/// <param name="obj"></param>
public void DeRegister(object obj)
{
List<string> funcNameList = new List<string>();
foreach (KeyValuePair<string, List<Pair>> kv in eventsDic)
{
var values = kv.Value;
for (int i = 0; i < values.Count; i++)
{
if (values[i].obj.Equals(obj))
{
values.RemoveAt(i);
continue;
}
if (values.Count == 0)
{
funcNameList.Add(kv.Key);
}
}
//从字典中移除List<Pair>为空的键值
foreach (string s in funcNameList)
{
if (eventsDic.ContainsKey(s))
{
eventsDic.Remove(s);
}
}
}
}
/// <summary>
/// 调用时间
/// </summary>
/// <param name="eventName"></param>
/// <param name="e"></param>
public void Fire(string eventName, Args e)
{
if (eventsDic.ContainsKey(eventName))
{
foreach (Pair pair in eventsDic[eventName])
{
if (pair.obj != null && pair.deleget != null)
{
pair.deleget(e);
}
}
}
else
{
Debug.LogError("没有注册过该事件");
}
}
}
0 0
- 观察者 (事件委托)
- 观察者模式,事件,委托
- C#观察者模式-事件委托
- 委托 事件 实现观察者模式
- 设计模式------观察者模式(ObserverPattern)和委托事件
- 观察者设计模式 Vs 事件委托(java)
- 观察者模式-猫捉老鼠(委托与事件)
- 观察者设计模式 Vs 事件委托(java)
- 观察者设计模式(C#委托和事件的使用)
- 观察者设计模式 Vs 事件委托(java)
- C# 观察者模式和委托、事件
- C#中委托,事件以及观察者模式
- C#委托和事件 观察者模式理解
- c#委托和事件及观察者模式
- 委托和事件——观察者模式
- 设计模式 之 观察者--委托与事件
- 观察者模式——委托与事件
- C# 中的委托和事件 观察者模式
- SQLServer创建链接服务器
- java 进行文件的压缩,解压,删除,拷贝操作
- CFileDialog如何使用lpstrFilter创建文件
- 随机抽取到数组
- LeetCode 12. Integer to Roman 解题报告
- 观察者 (事件委托)
- 布匹破损检测与自动缝补系统(佛山海天调味品有限公司)
- CreateProcess函数使用
- IT问题日记1
- 分布式随笔1-分布式概述
- SublimeText比较文件插件Diffy用法
- CfileDialog不能依据选择的filter类型自动推导扩展名
- dispatch_group项目中使用
- oracle默认初始化用户名密码和密码修改