观察者模式:定义了对象之间的一对多依赖。这样一来,当一个对象改变状态时,他的所有依赖者都会收到通知并自动更新。
观察者模式:定义了对象之间的一对多依赖。这样一来,当一个对象改变状态时,他的所有依赖者都会收到通知并自动更新。
![]()
Observer_Patterns
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Collections ;
5![]()
6
namespace Observer_Patterns
7![]()
![]()
{
8
public class Program
9![]()
{
10
static void Main(string[] args)
11![]()
{
12
WeatherData weatherData = new WeatherData();
13
CurrentCoditionsDisplay currentDisplay = new CurrentCoditionsDisplay(weatherData);
14![]()
15
weatherData.setMeasurements(80,65,30.4f);
16
weatherData.setMeasurements(82, 70, 29.2f);
17
weatherData.setMeasurements(78,90,29.2f);
18
}
19
}
20![]()
21![]()
/**//// <summary>
22
/// 主题接口,用来注册或者删除观察者
23
/// </summary>
24
public interface ISubject
25![]()
{
26
void RegisterObserver(IObserver o);
27
void RemoveObserver(IObserver o);
28
void NotifyObservers();
29
}
30![]()
/**//// <summary>
31
/// 观察者接口,所有观察者都必须实现update方法
32
/// </summary>
33
public interface IObserver
34![]()
{
35
void update(float temperature, float humidity, float pressure);
36
}
37![]()
/**//// <summary>
38
/// 布告板接口,
39
/// </summary>
40
public interface IDisplayElement
41![]()
{
42
void display();
43
}
44![]()
45![]()
/**//// <summary>
46
/// 实现气象站
47
/// </summary>
48
public class WeatherData : ISubject
49![]()
{
50
private ArrayList observers;
51
private float temperature;
52
private float humidity;
53
private float pressure;
54![]()
55
public WeatherData()
56![]()
{
57
observers = new ArrayList();
58
}
59![]()
60
public void RegisterObserver(IObserver o)
61![]()
{
62
observers.Add(o);
63
}
64![]()
65
public void RemoveObserver(IObserver o)
66![]()
{
67
int i = observers.IndexOf(o);
68
if (i > 0)
69![]()
{
70
observers.Remove(i);
71
}
72
}
73![]()
74
public void NotifyObservers()
75![]()
{
76
for (int i = 0; i < observers.Count; i++)
77![]()
{
78
IObserver observer = (IObserver)observers[i];
79
observer.update(this.temperature,this.humidity,this.pressure );
80
}
81
}
82![]()
83
public void MeasurementsChanged()
84![]()
{
85
NotifyObservers();
86
}
87![]()
88
public void setMeasurements(float temperature, float humidity, float pressure)
89![]()
{
90
this.temperature = temperature;
91
this.humidity = humidity;
92
this.pressure = pressure;
93
MeasurementsChanged();
94
}
95
}
96![]()
97![]()
/**//// <summary>
98
/// 一个布告板的实现
99
/// </summary>
100
public class CurrentCoditionsDisplay : IObserver, IDisplayElement
101![]()
{
102
private float temperature;
103
private float humidity;
104
private ISubject weatherData;
105![]()
106
public CurrentCoditionsDisplay(ISubject weatherData)
107![]()
{
108
this.weatherData = weatherData;
109
weatherData.RegisterObserver(this);
110
}
111![]()
112
public void update(float temperature, float humidity, float pressure)
113![]()
{
114
this.temperature = temperature;
115
this.humidity = humidity;
116
117
display();
118
}
119
public void display()
120![]()
{
121
Console.WriteLine("Current Conditions: " + temperature.ToString()
122
+" F degrees and "+humidity.ToString()
123
+" % humidity" );
124
}
125![]()
126
}
127![]()
128
}
129![]()
1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Collections ;5

6
namespace Observer_Patterns7


{8
public class Program9

{10
static void Main(string[] args)11

{12
WeatherData weatherData = new WeatherData();13
CurrentCoditionsDisplay currentDisplay = new CurrentCoditionsDisplay(weatherData);14

15
weatherData.setMeasurements(80,65,30.4f);16
weatherData.setMeasurements(82, 70, 29.2f);17
weatherData.setMeasurements(78,90,29.2f);18
}19
}20

21

/**//// <summary>22
/// 主题接口,用来注册或者删除观察者23
/// </summary>24
public interface ISubject25

{26
void RegisterObserver(IObserver o);27
void RemoveObserver(IObserver o);28
void NotifyObservers();29
}30

/**//// <summary>31
/// 观察者接口,所有观察者都必须实现update方法32
/// </summary>33
public interface IObserver34

{35
void update(float temperature, float humidity, float pressure);36
}37

/**//// <summary>38
/// 布告板接口,39
/// </summary>40
public interface IDisplayElement41

{42
void display();43
}44

45

/**//// <summary>46
/// 实现气象站47
/// </summary>48
public class WeatherData : ISubject49

{50
private ArrayList observers;51
private float temperature;52
private float humidity;53
private float pressure;54

55
public WeatherData()56

{57
observers = new ArrayList();58
}59

60
public void RegisterObserver(IObserver o)61

{62
observers.Add(o);63
}64

65
public void RemoveObserver(IObserver o)66

{67
int i = observers.IndexOf(o);68
if (i > 0)69

{70
observers.Remove(i);71
}72
}73

74
public void NotifyObservers()75

{76
for (int i = 0; i < observers.Count; i++)77

{78
IObserver observer = (IObserver)observers[i];79
observer.update(this.temperature,this.humidity,this.pressure );80
}81
}82

83
public void MeasurementsChanged()84

{85
NotifyObservers();86
}87

88
public void setMeasurements(float temperature, float humidity, float pressure)89

{90
this.temperature = temperature;91
this.humidity = humidity;92
this.pressure = pressure;93
MeasurementsChanged();94
}95
}96

97

/**//// <summary>98
/// 一个布告板的实现99
/// </summary>100
public class CurrentCoditionsDisplay : IObserver, IDisplayElement101

{102
private float temperature;103
private float humidity;104
private ISubject weatherData;105

106
public CurrentCoditionsDisplay(ISubject weatherData)107

{108
this.weatherData = weatherData;109
weatherData.RegisterObserver(this);110
}111

112
public void update(float temperature, float humidity, float pressure)113

{114
this.temperature = temperature;115
this.humidity = humidity;116
117
display();118
}119
public void display()120

{121
Console.WriteLine("Current Conditions: " + temperature.ToString() 122
+" F degrees and "+humidity.ToString() 123
+" % humidity" );124
}125

126
}127

128
}129

浙公网安备 33010602011771号