解耦广播系统用委托+字典实现,C#

 

 

这个和观察者模式很像。 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventCenter  {
   static Dictionary<EventType,Delegate> EventsDic=new Dictionary<EventType, Delegate>();

    #region 无参数 
    //无参数
    public static void AddListner(EventType eventType,CallBack callBack )
    {
        OnListenerAdding(eventType, callBack);
        EventsDic[eventType] = (CallBack)EventsDic[eventType] + callBack;
    }
    

    public static void RemoveListner(EventType eventType,CallBack callBack)
    {
        OnListenerRemoveing(eventType, callBack);
        EventsDic[eventType] =(CallBack) EventsDic[eventType] - callBack;
        OnListenerRemoved(eventType);
    }

    public static void BroadCast(EventType eventType )
    {

        Delegate d;
        if (EventsDic.TryGetValue(eventType,out d))
        {
            CallBack callBack = d as CallBack;
            if (callBack == null)
            {
                throw new Exception(string.Format("要广播的事件{0}的委托为空",EventsDic[eventType]));
            }
            else
            {
                callBack();
            }
        }
        else
        {
            throw new Exception(string.Format("不存在事件号:{0}", eventType));
        }
         
    }
    #endregion

    private static void OnListenerAdding(EventType eventType, Delegate callBack)
    {
        if (!EventsDic.ContainsKey(eventType))
        {
            EventsDic.Add(eventType, null);
        }
        Delegate d = EventsDic[eventType];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("参数类型不匹配无法添加,要添加的委托是{0},它的参数类型是{1},添加的参数类型是{2}", EventsDic[eventType], d.GetType(), callBack.GetType()));
        }
    }
    private static void OnListenerRemoveing(EventType eventType, Delegate callBack)
    {
        if (EventsDic.ContainsKey(eventType))
        {
            if (EventsDic[eventType] == null)
            {
                throw new Exception(string.Format("事件{0}为空", EventsDic[eventType]));
            }
            if (EventsDic[eventType].GetType() != callBack.GetType())
            {

                throw new Exception(string.Format("事件{0}的委托类型不匹配{0}要添加的委托事件是:{1}", EventsDic[eventType].GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("不存在事件号:{0}", eventType));
        }
    }
    private static void OnListenerRemoved(EventType eventType)
    {
        if (EventsDic[eventType] == null)
        {
            EventsDic.Remove(eventType);
        }
    }
    #region 一个参数
    //一个 参数
    public static void AddListner<T>(EventType eventType, CallBack<T> callBack)
    {
        OnListenerAdding(eventType, callBack);
        EventsDic[eventType] = (CallBack<T>)EventsDic[eventType] + callBack;
    }
    public static void RemoveListner<T>(EventType eventType, CallBack<T> callBack)
    {
        OnListenerRemoveing(eventType, callBack);
        EventsDic[eventType] = (CallBack<T>)EventsDic[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    public static void BroadCast<T>(EventType eventType,T arg1)
    {

        Delegate d;
        if (EventsDic.TryGetValue(eventType, out d))
        {
            CallBack<T> callBack = d as CallBack<T>;
            if (callBack == null)
            {
                throw new Exception(string.Format("要广播的事件{0}的委托为空", EventsDic[eventType]));
            }
            else
            {
                callBack(arg1);
            }
        }
        else
        {
            throw new Exception(string.Format("不存在事件号:{0}", eventType));
        }

    }
    #endregion

    #region 两个参数
    // 两个 参数
    public static void AddListner<T,X>(EventType eventType, CallBack<T,X> callBack)
    {
        OnListenerAdding(eventType, callBack);
        EventsDic[eventType] = (CallBack<T,X>)EventsDic[eventType] + callBack;
    }
    public static void RemoveListner<T,X>(EventType eventType, CallBack<T,X> callBack)
    {
        OnListenerRemoveing(eventType, callBack);
        EventsDic[eventType] = (CallBack<T,X>)EventsDic[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    public static void BroadCast<T, X>(EventType eventType, T arg1, X arg2)
    {

        Delegate d;
        if (EventsDic.TryGetValue(eventType, out d))
        {
            CallBack<T, X> callBack = d as CallBack<T, X>;
            if (callBack == null)
            {
                throw new Exception(string.Format("要广播的事件{0}的委托为空", EventsDic[eventType]));
            }
            else
            {
                callBack(arg1, arg2);
            }
        }
        else
        {
            throw new Exception(string.Format("不存在事件号:{0}", eventType));
        }

    }
    #endregion
    #region 三个参数
    // 三个 参数
    public static void AddListner<T, X,Y>(EventType eventType, CallBack<T, X,Y> callBack)
    {
        OnListenerAdding(eventType, callBack);
        EventsDic[eventType] = (CallBack<T, X,Y>)EventsDic[eventType] + callBack;
    }
    public static void RemoveListner<T, X,Y>(EventType eventType, CallBack<T, X,Y> callBack)
    {
        OnListenerRemoveing(eventType, callBack);
        EventsDic[eventType] = (CallBack<T, X, Y>)EventsDic[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    public static void BroadCast<T, X,Y>(EventType eventType, T arg1, X arg2,Y arg3)
    {

        Delegate d;
        if (EventsDic.TryGetValue(eventType, out d))
        {
            CallBack<T, X,Y> callBack = d as CallBack<T, X,Y>;
            if (callBack == null)
            {
                throw new Exception(string.Format("要广播的事件{0}的委托为空", EventsDic[eventType]));
            }
            else
            {
                callBack(arg1, arg2,arg3);
            }
        }
        else
        {
            throw new Exception(string.Format("不存在事件号:{0}", eventType));
        }

    }
    #endregion
    #region 四个参数
    // 四个 参数
    public static void AddListner<T, X, Y,Z>(EventType eventType,  CallBack<T, X, Y, Z> callBack)
    {
        OnListenerAdding(eventType, callBack);
        EventsDic[eventType] = (CallBack<T, X, Y,Z>)EventsDic[eventType] + callBack;
    }
    public static void RemoveListner<T, X, Y,Z>(EventType eventType, CallBack<T, X, Y,Z> callBack)
    {
        OnListenerRemoveing(eventType, callBack);
        EventsDic[eventType] = (CallBack<T, X, Y,Z>)EventsDic[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    public static void BroadCast<T, X, Y,Z>(EventType eventType, T arg1, X arg2, Y arg3,Z arg4)
    {

        Delegate d;
        if (EventsDic.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y,Z> callBack = d as CallBack<T, X, Y,Z>;
            if (callBack == null)
            {
                throw new Exception(string.Format("要广播的事件{0}的委托为空", EventsDic[eventType]));
            }
            else
            {
                callBack(arg1, arg2, arg3,arg4);
            }
        }
        else
        {
            throw new Exception(string.Format("不存在事件号:{0}", eventType));
        }

    }
    #endregion
    #region 五个参数
    // 五个 参数
    public static void AddListner<T, X, Y, Z,A>(EventType eventType, CallBack<T, X, Y, Z,A> callBack)
    {
        OnListenerAdding(eventType, callBack);
        EventsDic[eventType] = (CallBack<T, X, Y, Z,A>)EventsDic[eventType] + callBack;
    }
    public static void RemoveListner<T, X, Y, Z,A>(EventType eventType, CallBack<T, X, Y, Z,A> callBack)
    {
        OnListenerRemoveing(eventType, callBack);
        EventsDic[eventType] = (CallBack<T, X, Y, Z,A>)EventsDic[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    public static void BroadCast<T, X, Y, Z,A>(EventType eventType, T arg1, X arg2, Y arg3, Z arg4,A arg5)
    {

        Delegate d;
        if (EventsDic.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y, Z,A> callBack = d as CallBack<T, X, Y, Z,A>;
            if (callBack == null)
            {
                throw new Exception(string.Format("要广播的事件{0}的委托为空", EventsDic[eventType]));
            }
            else
            {
                callBack(arg1, arg2, arg3, arg4,arg5);
            }
        }
        else
        {
            throw new Exception(string.Format("不存在事件号:{0}", eventType));
        }

    }
    #endregion
}
 
public enum EventType
{
    ShowText
}

 

public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T, X>(T arg1, X arg2);
public delegate void CallBack<T, X, Y>(T agr1, X arg2, Y arg3);
public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
public delegate void CallBack<T, X, Y, Z, A>(T arg1, X arg2, Y arg3, Z arg4, A arg5);


客服端代码  挂载在Button上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MyTest : MonoBehaviour {

    public Text Text;
    private void Awake()
    {
        EventCenter.AddListner<string>(EventType.ShowText, (string a) =>
        {
            Text.gameObject.SetActive(true);
            Text.text = a;
        });

        GetComponent<Button>().onClick.AddListener(() => { EventCenter.BroadCast(EventType.ShowText, "EventCent hellow "); });

       Text.gameObject.SetActive(false);
    }


    private void OnDestroy()
    {
         
    }
}

 

有了上面的广播 系统, 各功能的脚本联系 就会那么紧密了,标题是高度解耦,缺点也是在于提示不太友好,

观察者模式 是 抽象主题 和 抽象观察者间 的联系。

 

 之前写过一篇观察者模式的文章,  思路和这个是一样的。都是利用委托 来和字典来实现的。

 

 

 

 

以上是学习http://www.sikiedu.com/course/304/task/16304/show 的总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值