Beispiel #1
0
        static void Main(string[] args)
        {
            #region 过程演示代码
            //// 前台小姐童子喆
            //Secretary tongzizhe = new Secretary();
            //// 看股票的同事
            //StockObserver tongshi1 = new StockObserver("魏观姹", tongzizhe);
            //StockObserver tongshi2 = new StockObserver("易管查", tongzizhe);

            //// 前台记下了两位同事
            //tongzizhe.Attach(tongshi1);
            //tongzizhe.Attach(tongshi2);
            //// 发现老板回来
            //tongzizhe.SecretaryAction = "老板回来了!";
            //// 通知两个同事
            //tongzizhe.Notify();
            #endregion

            #region 使用观察者模式代码
            // 老板胡汉三
            //Boss huhansan = new Boss();
            //// 看股票的同事
            //StockObserver tongshi1 = new StockObserver("魏观姹", huhansan);
            //// 看NBA的同事
            //NBAObserver tongshi2 = new NBAObserver("易管查", huhansan);
            //huhansan.Attach(tongshi1);
            //huhansan.Attach(tongshi2);

            //// 同事二未被通知到,移除
            //huhansan.Detach(tongshi1);

            //// 老板回来
            //huhansan.SubjectState = "我胡汉三回来了!";
            //// 发出通知
            //huhansan.Notify();
            #endregion

            //老板胡汉三
            Boss      huhansan  = new Boss();
            Secretary secretary = new Secretary();
            // 看股票的同事
            StockObserver tongshi1 = new StockObserver("魏观姹", huhansan);
            // 看NBA的同事
            NBAObserver tongshi2 = new NBAObserver("易管查", secretary);

            huhansan.Update  += tongshi1.CloseStockMarket;
            secretary.Update += tongshi2.CloseNBADirectSeeding;

            // 老板回来
            huhansan.SubjectState = "我胡汉三回来了!";
            // 发出通知
            huhansan.Notify();
            // 前台报告
            secretary.SubjectState = "老板回来了!";
            // 发出通知
            secretary.Notify();

            Console.Read();
        }
        static void Main(string[] args)
        {
            Boss          huhansan = new Boss();
            StockObserver tongshi1 = new StockObserver("魏关", huhansan);
            NBAObserver   tongshi2 = new NBAObserver("关", huhansan);

            huhansan.Update      += new Boss.EventHandler(tongshi1.ColseStockMarket);
            huhansan.Update      += new Boss.EventHandler(tongshi2.ColseStockMarket);
            huhansan.SubjectState = "我回来了";
            huhansan.Notify();
            Console.Read();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Boss huhansan = new Boss();                                  //老板胡汉三

            StockObserver tongshi1 = new StockObserver("魏关姹", huhansan); //看股票的同事
            NBAObserver   tongshi2 = new NBAObserver("易管查", huhansan);   //看NBA的同事

            huhansan.Update += new EventHandler(tongshi1.CloseStockMarket);
            huhansan.Update += new EventHandler(tongshi2.CloseNBADirectSeeding);

            huhansan.SubjectState = "我胡汉三回来了";

            huhansan.Notify();

            Console.ReadLine();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            //前台小姐童子喆
            Secretary tongzizhe = new Secretary();
            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", tongzizhe);
            //看NBA的同事
            NBAObserver tongshi2 = new NBAObserver("易管查", tongzizhe);

            //前台记下了两位同事
            tongzizhe.Attach(tongshi1);
            tongzizhe.Attach(tongshi2);
            //发现老板回来
            tongzizhe.SecretaryAction = "老板回来了!";
            //通知两个同事
            tongzizhe.Notify();

            Console.Read();
        }
Beispiel #5
0
        private static void Main(string[] args)
        {
            //前台小姐童子喆
            Secretary tongzizhe = new Secretary();
            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", tongzizhe);
            //看NBA的同事
            NBAObserver tongshi2 = new NBAObserver("易管查", tongzizhe);

            //前台记下了两位同事
            tongzizhe.Attach(tongshi1);
            tongzizhe.Attach(tongshi2);
            //发现老板回来
            tongzizhe.SecretaryAction = "老板回来了!";
            //通知两个同事
            tongzizhe.Notify();

            Console.Read();
        }
        private static void Main(string[] args)
        {
            //老板胡汉三
            Boss huhansan = new Boss();

            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", huhansan);
            //看NBA的同事
            NBAObserver tongshi2 = new NBAObserver("易管查", huhansan);

            huhansan.Attach(tongshi1);
            huhansan.Attach(tongshi2);

            huhansan.Detach(tongshi1);

            //老板回来
            huhansan.SubjectState = "我胡汉三回来了!";
            //发出通知
            huhansan.Notify();

            Console.Read();
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            //老板胡汉三
            Boss huhansan = new Boss();

            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", huhansan);
            //看NBA的同事
            NBAObserver tongshi2 = new NBAObserver("易管查", huhansan);

            huhansan.Attach(tongshi1);
            huhansan.Attach(tongshi2);

            huhansan.Detach(tongshi1);

            //老板回来
            huhansan.SubjectState = "我胡汉三回来了!";
            //发出通知
            huhansan.Notify();

            Console.Read();
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            //老板胡汉三
            Boss huhansan = new Boss();

            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", huhansan);
            //看NBA的同事
            NBAObserver tongshi2 = new NBAObserver("易管查", huhansan);

            huhansan.Update += new EventHandler(tongshi1.CloseStockMarket);
            huhansan.Update += new EventHandler(tongshi2.CloseNBADirectSeeding);

            //老板回来
            huhansan.SubjectState = "我胡汉三回来了!";
            //发出通知
            huhansan.Notify();

            Console.Read();


        }
Beispiel #9
0
 static void Main(string[] args)
 {
     Boss boss = new Boss();
     NBAObserver o1 = new NBAObserver("小明",boss);
     StockObserver o2 = new StockObserver("张建",boss);
     boss.SubjectState = "老板来了";
     boss.Update += new EventHandler(o1.CloseNBA);
     boss.Update += new EventHandler(o2.CloseStock);
     boss.Notify();
 }