• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Life is Good.

            Enhance Tech and English
            隨筆 - 65, 文章 - 20, 評論 - 21, 引用 - 0
            數據加載中……

            Observer

            名稱 Observer
            結構

            意圖 定義對象間的一種一對多的依賴關系,當一個對象的狀態發生改變時, 所有依賴于它的對象都得到通知并被自動更新。
            適用性
            • 當一個抽象模型有兩個方面, 其中一個方面依賴于另一方面。將這二者封裝在獨立的對象中以使它們可以各自獨立地改變和復用。
            • 當對一個對象的改變需要同時改變其它對象, 而不知道具體有多少對象有待改變。
            • 當一個對象必須通知其它對象,而它又不能假定其它對象是誰。換言之, 你不希望這些對象是緊密耦合的。



            namespace Observer
            {
              
            public abstract class Stock // Subject
              {
                
            protected string name;
                
            protected double price;
                
            private ArrayList inventors = new ArrayList();

                
            public Stock(string name, double price)
                {
                  
            this.name = name;
                  
            this.price = price;
                }

                
            public void Attach(Investor investor)
                {
                  inventors.Add(investor);
                }

                
            public void Detach(Investor investor)
                {
                  inventors.Remove(investor);
                }

                
            public void Notify()
                {
                  
            foreach (Investor investor in inventors)
                  {
                    investor.Update(
            this);
                  }
                }

                
            // Properties 
                public double Price
                {
                  
            get { return price; }
                  
            set
                  {
                    price 
            = value;
                    Notify();
                  }
                }

                
            public Investor Investor
                {
                  
            get
                  {
                    
            throw new System.NotImplementedException();
                  }
                  
            set
                  {
                  }
                }
              }

              
            public class ADSK : Stock
              {
                
            public ADSK(string name, double price)
                  :
            base(name, price)
                {

                }

              }

              
            public class ABB : Stock
              {
                 
            public ABB(string name, double price)
                  :
            base(name, price)
                {

                }
              }
            }


            namespace Observer
            {
              
            public abstract class Investor // Observer
              {
                
            abstract public void Update(Stock stock);
              }

              
            public class SmallInvestor : Investor
              {
                
            private string name;
                
            public SmallInvestor(string name)
                {
                  
            this.name = name;
                }

                
            override public void Update(Stock stock)
                {
                  Console.WriteLine(
            "Small Investor is Notified! ");
                }
              }

              
            public class BigInvestor : Investor
              {
               
            private string name;
                
            public BigInvestor(string name)
                {
                  
            this.name = name;
                }
                
            override  public void Update(Stock stock)
                {
                  Console.WriteLine(
            "Big Investor is Notified! ");
                }
              }
            }


            namespace Observer
            {
              
            class Program
              {
                
            static void Main(string[] args)
                {
                  
            // Create investors/ Observers
                  SmallInvestor s = new SmallInvestor("Small Investor");
                  BigInvestor b 
            = new BigInvestor("Big Investor");

                  ADSK adsk 
            = new ADSK("ADSK"46.0);
                  ABB abb 
            = new ABB("ABB"23.4);

                  adsk.Attach(s);
                  adsk.Attach(b);

                  abb.Attach(s);
                  abb.Attach(b);

                  adsk.Price 
            = 48;
                  abb.Price 
            = 26;

                  
            return;
                }
              }
            }

            posted on 2011-06-11 22:32 Mike Song 閱讀(265) 評論(0)  編輯 收藏 引用 所屬分類: Design Pattern

            久久国产精品无码网站| 欧美日韩中文字幕久久久不卡 | 国产精品va久久久久久久| 国内精品久久久久久99| 久久精品国产亚洲网站| 国产精品成人99久久久久| 久久久久亚洲AV成人网人人软件| 精品久久久久久无码人妻蜜桃| 三级三级久久三级久久| 国产午夜免费高清久久影院| 国产无套内射久久久国产| 久久精品国产清自在天天线| 久久青青草原精品影院| 午夜精品久久久久久| 久久久久99精品成人片欧美| 99久久精品免费看国产一区二区三区| 精品久久久久久久国产潘金莲| 情人伊人久久综合亚洲| 99久久精品国产一区二区| 久久精品国产一区二区三区日韩| 狠狠色丁香久久婷婷综合_中| 97久久超碰成人精品网站| 亚洲人成无码www久久久| 99久久精品免费观看国产| 97久久超碰国产精品旧版| 精品国产青草久久久久福利| 亚洲精品乱码久久久久久不卡| 精品一区二区久久久久久久网站| 久久人人爽人人爽人人片AV麻烦| 久久人人爽人爽人人爽av| 亚洲国产二区三区久久| 99久久精品日本一区二区免费 | 色偷偷888欧美精品久久久| 久久精品久久久久观看99水蜜桃| 久久国产精品二国产精品| 久久精品中文字幕久久| www.久久热.com| 国产欧美一区二区久久| 久久久久亚洲AV成人片| 国产亚洲美女精品久久久2020| 亚洲中文字幕久久精品无码APP|