.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