Esempio n. 1
0
        private static void DelegateTest()
        {
            Console.WriteLine("委托实现的发布订阅模式来钓鱼:");
            Console.WriteLine("=======================");
            //1、初始化鱼竿
            var fishingRod = new FishingRodWithDelegate();

            //2、声明垂钓者
            var jeff = new FishingMan("圣杰");

            //3.分配鱼竿
            jeff.FishingRod = fishingRod;

            //4、注册观察者(已通过反射动态注册)
            //fishingRod.FishingEvent += new FishingEventHandler().HandleEvent;

            //5、循环钓鱼
            while (jeff.FishCount < 5)
            {
                jeff.Fishing();
                Console.WriteLine("-------------------");
                //睡眠2s
                Thread.Sleep(2000);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 下钩
        /// </summary>
        public void ThrowHook(FishingMan man)
        {
            Console.WriteLine("开始下钩!");

            //用随机数模拟鱼咬钩,若随机数为偶数,则为鱼咬钩
            if (new Random().Next() % 2 == 0)
            {
                var a    = new Random(10).Next();
                var type = (FishType) new Random().Next(0, 5);
                Console.WriteLine("铃铛:叮叮叮,鱼儿咬钩了");

                var eventData = new FishingEventData()
                {
                    FishType = type, FishingMan = man
                };

                FishingEvent?.Invoke(eventData);//通过事件委托触发
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 下钩
        /// </summary>
        public void ThrowHook(FishingMan man)
        {
            Console.WriteLine("开始下钩!");

            //用随机数模拟鱼咬钩,若随机数为偶数,则为鱼咬钩
            if (new Random().Next() % 2 == 0)
            {
                var a    = new Random(10).Next();
                var type = (FishType) new Random().Next(0, 5);
                Console.WriteLine("铃铛:叮叮叮,鱼儿咬钩了");

                var eventData = new FishingEventData()
                {
                    FishType = type, FishingMan = man
                };

                EventBus.EventBus.Default.Trigger <FishingEventData>(eventData);//直接通过事件总线触发
            }
        }
Esempio n. 4
0
        private static void EventBusTest()
        {
            Console.WriteLine("事件总线来钓鱼:");
            Console.WriteLine("=======================");
            //1、初始化鱼竿
            var fishingRod = new FishingRod();

            //2、声明垂钓者
            var jeff = new FishingMan("小明");

            //3.分配鱼竿
            jeff.FishingRod = fishingRod;

            //4、循环钓鱼
            while (jeff.FishCount < 5)
            {
                jeff.Fishing();
                Console.WriteLine("-------------------");
                //睡眠2s
                Thread.Sleep(2000);
            }
        }