广播消息

1. 接收消息和广播消息的类

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSGNotify.AppCode
{
    enum EventType
    {
        FireByA,
    }

    class BoardCast
    {
        private Dictionary<EventType ,int> m_Send = new Dictionary<EventType ,int>();
        private Dictionary<EventType, List<Action<int>>> m_Receive = new Dictionary<EventType, List<Action<int>>>();

        public void Init()
        {
            
        }

        private static BoardCast instance = new BoardCast();
        public static BoardCast Instance { get { return instance; } }

        public void RegisterGetFireEvent(EventType type, Action<int> _action)
        {
            if (!m_Receive.ContainsKey(type))
            {
                KeyValuePair<EventType, List<Action<int>>> pair = new KeyValuePair<EventType, List<Action<int>>>(type, new List<Action<int>>());
                m_Receive.Add(pair.Key, pair.Value);
            }

            m_Receive[type].Add(_action); 
        }

        public void RegisterOnFireEvent(EventType type, int msg)
        {
            m_Send[type] = msg;
        }

        public void ProcessEvent()
        {
            foreach (KeyValuePair<EventType,int> pair in m_Send)
            {
                List<Action<int>> actionList = new List<Action<int>>();
                m_Receive.TryGetValue(pair.Key, out actionList);

                foreach (Action<int> action in actionList)
                {
                    action.Invoke(pair.Value);
                }
            }

            m_Send.Clear();
        }
    }

}</span>

2. 测试类A 和B,A广播消息,B接收消息

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSGNotify.AppCode
{
    class A
    {
        public int x = 1;

        public A()
        {
           // Register();
        }

        public void Set(int _x)
        {
            x = _x;
            BoardCast.Instance.RegisterOnFireEvent(EventType.FireByA, x);
        }

        public void Print(int i)
        {
            Console.WriteLine("A{0}:{1}", i, x);
        }
    }
}
</span>

3. main测试

<span style="font-size:18px;">using MSGNotify.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSGNotify
{
    class Program
    {
        static void Main(string[] args)
        {
            //A a1 = new A();
            //A a2 = new A();

            //a1.Set(10);
            //a2.Set(20);
            A a = new A();
            B b = new B();

            BoardCast.Instance.ProcessEvent();
            Console.WriteLine("A never changed,A:{0}",a.x);
            //BoardCast.Instance.Process();

            a.Set(10);
            BoardCast.Instance.ProcessEvent();
        }
    }
}
</span>
4. 运行结果

5. 代码

http://download.csdn.net/detail/ywjun0919/7990735

你可能感兴趣的:(设计模式,C#)