Exemplo n.º 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();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Boss          boss = new Boss("胡汉三");
            StockObserver so1  = new StockObserver("阿三", boss);
            StockObserver so2  = new StockObserver("阿四", boss);

            boss.Update += so1.Updata;
            boss.Update += so2.Updata;
            boss.Notify();
            Console.ReadKey();
        }
        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();
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            //前台小姐童子喆
            Secretary tongzizhe = new Secretary();
            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", tongzizhe);
            StockObserver tongshi2 = new StockObserver("易管查", tongzizhe);

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

            Console.Read();
        }
Exemplo n.º 6
0
        private static void Main(string[] args)
        {
            //前台小姐童子喆
            Secretary tongzizhe = new Secretary();
            //看股票的同事
            StockObserver tongshi1 = new StockObserver("魏关姹", tongzizhe);
            StockObserver tongshi2 = new StockObserver("易管查", 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();
        }
Exemplo n.º 8
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();
        }
Exemplo n.º 9
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();


        }
Exemplo n.º 10
0
 //减少
 public void Detach(StockObserver observer)
 {
     observers.Remove(observer);
 }
Exemplo n.º 11
0
 //增加
 public void Attach(StockObserver observer)
 {
     observers.Add(observer);
 }
Exemplo n.º 12
0
 //减少
 public void Detach(StockObserver observer)
 {
     observers.Remove(observer);
 }
Exemplo n.º 13
0
 //增加
 public void Attach(StockObserver observer)
 {
     observers.Add(observer);
 }
Exemplo n.º 14
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();
 }