Exemplo n.º 1
0
        // 观察者模式 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动更新。
        static void Observer()
        {
            INotifyer notifyer = new Secretary();

            Observer observer1 = new StockObserver("张三", notifyer);
            Observer observer2 = new StockObserver("魏观察", notifyer);

            notifyer.Attach(observer1);
            notifyer.Attach(observer2);

            notifyer.Detach(observer2);
            notifyer.NotifyerState = "老板回来了";
            notifyer.Notify();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            StockGrabber stockGrabber = new StockGrabber();

            var stockObserver1 = new StockObserver(stockGrabber);

            stockGrabber.setIbmPrice(197.00);
            stockGrabber.setaaplPrice(677.60);
            stockGrabber.setgoogPrice(676.40);

            var stockObserver2 = new StockObserver(stockGrabber);

            stockGrabber.setIbmPrice(97.00);
            stockGrabber.setaaplPrice(77.60);
            stockGrabber.setgoogPrice(76.40);


            stockGrabber.Unregister(stockObserver1);
        }