zoukankan      html  css  js  c++  java
  • Head First 设计模式----ObserverPattern

    观察者模式在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Observer
    {
        public interface Subject
        {
             void registerObserver(Observer o);
             void removeObserver(Observer o);
             void notifyObservers();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Observer
    {
       public interface Observer
        {
             void update(float temp, float humidity, float pressure);
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Observer
    {
       public interface DisplayElement
        {
             void display();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace HeadFirst.Observer
    { 
    
        class WeatherData:Subject
        {
            private ArrayList observers;
            private float temperature;
            private float humidity;
            private float pressure;
    
            public WeatherData() { 
                observers=new ArrayList();
            }
    
            public void registerObserver(Observer o) {
                observers.Add(o);
            }
    
            public void removeObserver(Observer o) {
                int i = observers.IndexOf(o);
                if(i>=0){
                    observers.Remove(i);
                }        
            }
    
            public void notifyObservers()
            {
                for (int i = 0; i < observers.Count; i++)
                {
                    Observer observer = (Observer)observers[i];
                    observer.update(temperature, humidity, pressure);
                }
            
            }
    
            public void measurementsChanged() {
                notifyObservers();
            }
    
            public void setMeasurements(float temperature,float humidity,float pressure) {
                this.temperature = temperature;
                this.humidity = humidity;
                this.pressure = pressure;
                measurementsChanged();        
            }
            //WeatherData的其它方法
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Observer
    {    //目前状况布告板
        class CurrentConditionsDisplay:Observer,DisplayElement
        {
            private float temperature;
            private float humidity;
            private Subject weatherData;
            public CurrentConditionsDisplay(Subject weatherData) {
                this.weatherData = weatherData;
                weatherData.registerObserver(this);        
            }
            public void update(float temperature, float humidity, float pressure) {
                this.temperature = temperature;
                this.humidity = humidity;
                display();
            }
            public void display() {
                Console.Write("Current conditions:"+temperature+" F degrees and "+humidity+" % humidity");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using HeadFirst.Decorator;
    using HeadFirst.Command;
    using HeadFirst.Observer;
    
    
    namespace HeadFirst
    {
        class Program
        {
            static void Main(string[] args)
            {
              
                //观察者模式
                WeatherData weatherdata = new WeatherData();
                CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherdata);
                weatherdata.setMeasurements(80, 65, 30.4f);
                Console.ReadLine();
                
            }
        }
    }
  • 相关阅读:
    PS常用美化处理方法大全
    FastReport.Net使用:[32]对话框使用2
    FastReport.Net使用:[31]使用带参查询及存储
    FastReport.Net使用:[30]对话框使用
    FastReport.Net使用:[29]调用存储过程1
    FastReport.Net使用:[28]数据合并
    FastReport.Net使用:[27]样式使用
    FastReport.Net使用:[26]数字格式
    FastReport.Net使用:[25]除数0处理方法
    FastReport.Net使用:[24]其他控件(邮政编码(Zip Code),网格文本(Cellular Text)以及线性刻度尺(Linear Gauge))
  • 原文地址:https://www.cnblogs.com/SmileX/p/4202112.html
Copyright © 2011-2022 走看看