.NET EventBus


需求:希望將自定義事件重代碼中解耦,單獨管理起來

代碼如下:

 public class EventBus<T>
    {
        private EventBus()
        {

        }

        private static EventBus<T> Eve { get; set; }

        public static EventBus<T> CreateInstance()
        {
            if (Eve == null)
            {
                Eve = new EventBus<T>();
            }
            return Eve;
        }

        private Dictionary<string, List<EventHandler>> dic = new Dictionary<string, List<EventHandler>>();

        public void InvockEvent(string eventName)
        {
            if (!Exist(eventName))
            {
                return;
            }
            foreach (var eventHandler in dic[eventName])
            {
                eventHandler.BeginInvoke(this, new EventArgs(), null, null);
            }
        }

        public void AddEvent(string eventName, EventHandler e)
        {
            if (Exist(eventName))
            {
                dic[eventName].Add(e);
            }
            else
            {
                List<EventHandler> list = new List<EventHandler>();
                list.Add(e);
                dic.Add(eventName, list);
            }
        }

        public void RemoveEvent(string eventName)
        {
            if (!Exist(eventName))
            {
                return;
            }
            dic.Remove(eventName);
        }

        public void UpdateEvent(string eventName, EventHandler e)
        {
            RemoveEvent(eventName);
            AddEvent(eventName, e);
        }

        public void AddEventList(string eventName, List<EventHandler> es)
        {
            if (Exist(eventName))
            {
                dic[eventName].AddRange(es);
            }
            else
            {
                List<EventHandler> list = new List<EventHandler>();
                list.AddRange(es);
                dic.Add(eventName, list);
            }
        }

        public void RemoveByhandle(string eventName, EventHandler e)
        {
            if (!Exist(eventName))
            {
                return;
            }
            dic[eventName].Where(c => c == e).ToList().Remove(e);
        }

        private bool Exist(string key)
        {
            return dic.Keys.Contains(key);
        }
    }

沒有做深入測試,只是一個大概意思,將event緩存起來,需要的時候,在取出來使用,其實這里

EventBus就算是一個發布者了,然后我們可以寫一個訂閱者:

 public class InitEvent
    {
        public static EventBus<MyString> ev = EventBus<MyString>.CreateInstance();

        static InitEvent()
        {
            ev.AddEvent("valueChange", (o, e) =>
            {
                Console.WriteLine(1111111);
            });
            ev.AddEvent("valueLengthChange", (o, e) =>
            {
                Console.WriteLine("長度變了");
            });
        }
    }

再寫一個使用的:

 public class MyString
    {
        private EventBus<MyString> ev = EventBus<MyString>.CreateInstance();
        private string Str { get; set; }

        public MyString(string str)
        {

            Str = str;
        }

        public string str
        {
            get { return Str; }
            set
            {
                if (value != Str)
                {
                    ev.InvockEvent("valueChange");
                }
                if (value.Length != Str.Length)
                {
                    ev.InvockEvent("valueLengthChange");
                }
                Str = value;
            }
        }
    }

這樣就沒有必要在MyString中寫事件了,達到解耦的目的,我們只需要在InitEvent中寫eventhandler就可以了

來測試一下:

 static void Main(string[] args)
        {
            InitEvent evv = new InitEvent();
            MyString ms = new MyString("haha");
            ms.str = "ffff";
            Console.ReadKey();
        }

輸出結果:

1111111

換下ms.str,使其長度發生變化:

static void Main(string[] args)
        {
            InitEvent evv = new InitEvent();
            MyString ms = new MyString("haha");
            ms.str = "ffff1";
            Console.ReadKey();
        }

輸出結果:

1111111
長度變了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM