Exemple #1
0
        public void ReceiveAndPrint(object obj)
        {
            TenXunGameNet tenxun = obj as TenXunGameNet;

            if (tenxun != null)
            {
                Console.WriteLine("Notified {0} of {1}'s" + " Info is: {2}", Name, tenxun.Symbol, tenxun.Info);
            }
        }
Exemple #2
0
        /// <summary>
        /// 观察者委托形式
        /// </summary>
        public void ObserverDeduceDelegate()
        {
            TenXunNet tenXunGameNet = new TenXunGameNet("TenXun Game", "Have a new game published ....");

            SubscriberNet lh  = new SubscriberNet("Learning Hard");
            SubscriberNet tom = new SubscriberNet("Tom");

            // 添加订阅者
            tenXunGameNet.AddObserver(new NotifyEventHandler(lh.ReceiveAndPrint));
            tenXunGameNet.AddObserver(new NotifyEventHandler(tom.ReceiveAndPrint));
            tenXunGameNet.Update();

            Console.WriteLine("-----------------------------------");
            Console.WriteLine("移除Tom订阅者");
            tenXunGameNet.RemoveObserver(new NotifyEventHandler(tom.ReceiveAndPrint));
            tenXunGameNet.Update();
        }