設計模式學習--觀察者模式

來源:互聯網
上載者:User

標籤:

觀察者模式定義了對象之間的一對多依賴,這樣一來,當一個對象改變狀態時,他的所有依賴著都會收到通知並自動更新。

 

設計原則 為了互動對象之間的松耦合設計而努力

 

  1 using System;  2 using System.Collections;  3 using System.Collections.Generic;  4 using System.Linq;  5 using System.Text;  6 using System.Threading.Tasks;  7   8 namespace ObserverPattern  9 { 10  11 //建立寄件者的介面 12     public interface IObserver 13     { 14         void update(float temp, float humidity, float pressure); 15     } 16 //主題介面,供註冊和解除監聽 17     public interface ISubject 18     { 19         void registerObserver(IObserver o); 20         void removeObserver(IObserver o); 21         void notifyObserver(); 22     } 23         //顯示介面 24     public interface IDisplayElement 25     { 26         void display(); 27     } 28  29 //源,實現監聽者的添加刪除以及發送訊息 30     public class WeatherData : ISubject 31     { 32         public ArrayList observers; 33         public float temperature; 34         public float humidity; 35         public float pressure; 36  37  38         public WeatherData() 39         { 40             observers = new ArrayList(); 41         } 42  43  44         public void registerObserver(IObserver o) 45         { 46             observers.Add(o); 47         } 48  49         public void removeObserver(IObserver o) 50         { 51             observers.Remove(o); 52         } 53 //給每個監聽者發送訊息 54         public void notifyObserver() 55         { 56             for (int i = 0; i < observers.Count; i++) 57             { 58                 (observers[i] as IObserver).update(temperature, humidity, pressure); 59             } 60         } 61  62 //觸發訊息 63         public void measurementsChanged() 64         { 65             notifyObserver(); 66         } 67 //設定訊息值 68         public void setMeasurements(float temperature, float humidity, float pressure) 69         { 70             this.temperature = temperature; 71             this.humidity = humidity; 72             this.pressure = pressure; 73  74             measurementsChanged(); 75         } 76  77     } 78  79 //監聽者之一 80     public class CurrentConditionsDisplay : IObserver, IDisplayElement 81     { 82  83  84  85         public float temperature; 86         public float humidity; 87         public float pressure; 88         private ISubject WeatherData; 89         public CurrentConditionsDisplay(ISubject WeatherData) 90         { 91             this.WeatherData = WeatherData; 92             WeatherData.registerObserver(this);//把自己註冊進去監聽 93         } 94  95 //weather會調用這個方法 96         public void update(float temp, float humidity, float pressure) 97         { 98             this.temperature = temp; 99             this.humidity = humidity;100             this.pressure = pressure;101             display();102         }103 //顯示104         public void display()105         {106             Console.WriteLine("Current conditions:" + temperature + "F degrees and " + humidity + "humidity");107         }108 109 110     }111 }
View Code

 

設計模式學習--觀察者模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.