Bootstrap

事件中心的例子

定义委托类型

public delegate void Callback();
public delegate void Callback<T>(T arg1);
public delegate void Callback<T, U>(T arg1, U arg2);

这里的参数数量可以继续添加,看需求。


public static Dictionary<EGameEvent, Delegate> mEventTable = new Dictionary<EGameEvent, Delegate>();

事件中心为一静态类,其中声明了以上这么一个事件字典,key是事件枚举,value是委托类型。


注册事件代码

public static void AddListener(EGameEvent eventType, Callback handler)
{
    BeforeAddListener(eventType, handler);
    mEventTable[eventType] = (Callback)mEventTable[eventType] + handler;
}

public static void AddListener<T>(EGameEvent eventType, Callback<T> handler)
{
    BeforeAddListener(eventType, handler);
    mEventTable[eventType] = (Callback<T>)mEventTable[eventType] + handler;
}

public static void AddListener<T, U>(EGameEvent eventType, Callback<T, U> handler)
{
    BeforeAddListener(eventType, handler);
    mEventTable[eventType] = (Callback<T, U>)mEventTable[eventType] + handler;
}

private static void BeforeAddListener(EGameEvent eventType, Delegate listenerBeingAdded)
{
    if (!mEventTable.ContainsKey(eventType))
    {
        mEventTable.Add(eventType, null);
    }

    Delegate d = mEventTable[eventType];
    if (d != null && d.GetType() != listenerBeingAdded.GetType())
    {
        throw new ListenerException(string.Format("尝试加入的委托实例与事件类型{0}的签名不一致,已经加入的委托类型为:{1},尝试加入的委托类型为{2}",eventType, d.GetType().Name, listenerBeingAdded.GetType().Name));
    }
}

注销事件代码

public static void RemoveListener(EGameEvent eventType, Callback handler)
{
    BeforeRemoveListener(eventType, handler);
    mEventTable[eventType] = (Callback)mEventTable[eventType] - handler;
    AfterRemoveListener(eventType);
}

public static void RemoveListener<T>(EGameEvent eventType, Callback<T> handler)
{
    BeforeRemoveListener(eventType, handler);
    mEventTable[eventType] = (Callback<T>)mEventTable[eventType] - handler;
    AfterRemoveListener(eventType);
}

public static void RemoveListener<T, U>(EGameEvent eventType, Callback<T, U> handler)
{
    BeforeRemoveListener(eventType, handler);
    mEventTable[eventType] = (Callback<T, U>)mEventTable[eventType] - handler;
    AfterRemoveListener(eventType);
}

private static void BeforeRemoveListener(EGameEvent eventType, Delegate listenerBeingRemoved)
{
    if (mEventTable.ContainsKey(eventType))
    {
        Delegate d = mEventTable[eventType];

        if (d == null)
        {
            throw new ListenerException(string.Format("尝试移除事件类型为:\"{0}\"的委托实例,但是此事件类型并未注册任何委托实例。", eventType));
        }
        else if (d.GetType() != listenerBeingRemoved.GetType())
        {
            throw new ListenerException(string.Format("尝试移除的委托实例与事件类型{0}的签名不一致,已经加入的委托类型为:{1},尝试移除的委托类型为{2}",eventType, d.GetType().Name,listenerBeingRemoved.GetType().Name));
        }
    }
    else
    {
        throw new ListenerException(string.Format("尝试移出事件类型为:\"{0}\"的委托实例,但是事件中心并未注册此事件类型。", eventType));
    }
}

private static void AfterRemoveListener(EGameEvent eventType)
{
    if (mEventTable[eventType] == null)
    {
        mEventTable.Remove(eventType);
    }
}

广播事件代码

public static void Broadcast(EGameEvent eventType)
{
    if (mEventTable.TryGetValue(eventType, out Delegate d))
    {
        if (d is Callback callback)
        {
            callback?.Invoke();
        }
        else
        {
            throw new BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType));
        }
    }
}

public static void Broadcast<T>(EGameEvent eventType, T arg1)
{
    if (mEventTable.TryGetValue(eventType, out Delegate d))
    {
        if (d is Callback<T> callback)
        {
            callback?.Invoke(arg1);
        }
        else
        {
            throw new BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType));
        }
    }
}
    
public static void Broadcast<T, U>(EGameEvent eventType, T arg1, U arg2)
{
    if (mEventTable.TryGetValue(eventType, out Delegate d))
    {
        if (d is Callback<T, U> callback)
        {
            callback?.Invoke(arg1, arg2);
        }
        else
        {
            throw new BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType));
        }
    }
}

 

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;