static void Main(string[] args) { TenXun tenXun = new TenXunGame("TenXun Game", "Have a new game published ...."); Subscriber lh = new Subscriber("Learning Hard"); Subscriber tom = new Subscriber("Tom"); // 添加订阅者 tenXun.AddObserver(new NotifyEventHandler(lh.ReceiveAndPrint)); tenXun.AddObserver(new NotifyEventHandler(tom.ReceiveAndPrint)); tenXun.Update(); Console.WriteLine("-----------------------------------"); TenXun newTenXun = new TenXun("helios"); newTenXun.AddObserver(new NotifyEventHandler(lh.sendMsg)); newTenXun.Update(); Console.WriteLine("-----------------------------------"); Console.WriteLine("移除Tom订阅者"); tenXun.RemoveObserver(new NotifyEventHandler(tom.ReceiveAndPrint)); tenXun.Update(); newTenXun.Update(); Console.ReadLine(); }
public void ReceiveAndPrint(Object obj) { TenXun tenxun = obj as TenXun; if (tenxun != null) { Console.WriteLine("Notified {0} of {1}'s" + " Info is: {2}", Name, tenxun.Symbol, tenxun.Info); } }
public void ReceiveAndPrint(TenXun tx) { Console.WriteLine("Notified {0} of {1}'s" + " Info is: {2}", Name, tx.Symbol, tx.Info); }