目的:分发各种事件到已经注册好事件的对象上边
关键点:
1.利用string,delegate的字典缓存好事件
2.搜索key值返回的delegate value去执行所有注册在委托下的事件
3.事件字典具备增删查功能
详细代码:
public delegate void ObjectParamDelegate(object obj);
public class BaseEvent
{
public void OnInitialiaze()
{
delegates = new Dictionary<string, ObjectParamDelegate>();
}
public void OnDestory()
{
delegates.Clear();
delegates = null;
}
public void AddEvent(string key, ObjectParamDelegate method)
{
if (!delegates.ContainsKey(key))
{
delegates.Add(key, method);
}
else
{
delegates[key] += method;
}
}
public void RemoveEvent(string key)
{
if (delegates.ContainsKey(key))
{
delegates.Remove(key);
}
}
public void DispatchEvent(string key, object obj = null)
{
if (delegates.ContainsKey(key))
{
delegates[key](obj);
}
}
public Dictionary<string, ObjectParamDelegate> delegates;
}