1
2{《HeadFirst设计模式》之观察者模式 }
3{ 主题与观察者 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :[email protected] }
6
7unit uWeatherReport;
8
9interface
10
11uses
12 Classes, SysUtils;
13
14type
15 TObserver = class; { Forward声明,创建两个相互依赖的类 }
16
17 { 抽象主题 }
18 TSubject = class(TObject)
19 procedure RegisterObserver(aObserver: TObserver); virtual; abstract;
20 procedure RemoveObserver (aObserver: TObserver); virtual; abstract;
21 procedure NotifyObserver; virtual; abstract;
22 end;
23
24 { 具体主题 }
25 TWeatherData = class(TSubject)
26 private
27 FObservers: TList;
28 FTemp : Integer;
29 FHumidity : Integer;
30 FPresssure: Integer;
31 public
32 constructor Create;
33 destructor Destroy; override;
34 procedure RegisterObserver(aObserver: TObserver); override;
35 procedure RemoveObserver (aObserver: TObserver); override;
36 procedure NotifyObserver; override;
37 procedure MeasurementsChanged;
38 procedure SetMeasurements(aTemp, aHumidity, aPresssure: Integer);
39 end;
40
41 {**************************************************************************}
42
43 { 抽象观察者 }
44 TObserver = class(TObject)
45 procedure UpDate(Temp, Humidity, Presssure: Integer); virtual; abstract;
46 procedure Display; virtual; abstract;
47 end;
48
49 { 具体观察者 }
50 TCurrentConditionsDisplay = class(TObserver)
51 private
52 FTemp : Integer;
53 FHumidity : Integer;
54 FWeatherData: TSubject;
55 public
56 constructor Create(aWeatherData: TSubject);
57 destructor Destroy; override;
58 procedure UpDate(aTemp, aHumidity, aPresssure: Integer); override;
59 procedure Display; override;
60 end;
61
62implementation
63
64{ TWeatherData }
65
66constructor TWeatherData.Create;
67begin
68 FObservers := TList.Create;
69end;
70
71destructor TWeatherData.Destroy;
72begin
73 FObservers.Clear;
74 FreeAndNil(FObservers);
75 inherited Destroy;
76end;
77
78procedure TWeatherData.MeasurementsChanged;
79begin
80 NotifyObserver;
81end;
82
83procedure TWeatherData.NotifyObserver;
84var
85 i: Integer;
86 aObserver: TObserver;
87begin
88 for i := 0 to FObservers.Count - 1 do
89 begin
90 aObserver := TObserver(FObservers.Items[i]);
91 aObserver.UpDate(FTemp, FHumidity, FPresssure);
92 end;
93end;
94
95procedure TWeatherData.RegisterObserver(aObserver: TObserver);
96begin
97 FObservers.Add(aObserver);
98end;
99
100procedure TWeatherData.RemoveObserver(aObserver: TObserver);
101var
102 i: Integer;
103begin
104 i := FObservers.IndexOf(aObserver);
105 if (i >= 0) then
106 FObservers.Remove(aObserver);
107end;
108
109procedure TWeatherData.SetMeasurements(aTemp, aHumidity, aPresssure: Integer);
110begin
111 FTemp := aTemp;
112 FHumidity := aHumidity;
113 FPresssure := aPresssure;
114 MeasurementsChanged;
115end;
116
117{ TCurrentConditionsDisplay }
118
119constructor TCurrentConditionsDisplay.Create(aWeatherData: TSubject);
120begin
121 FWeatherData := aWeatherData;
122 aWeatherData.RegisterObserver(Self);
123end;
124
125destructor TCurrentConditionsDisplay.Destroy;
126begin
127 inherited Destroy;
128end;
129
130procedure TCurrentConditionsDisplay.Display;
131begin
132 Writeln(‘Current Conditions: ‘, FTemp, ‘ degrees and ‘,
133 FHumidity, ‘% humidity.‘);
134end;
135
136procedure TCurrentConditionsDisplay.UpDate(aTemp, aHumidity,
137 aPresssure: Integer);
138begin
139 FTemp := aTemp;
140 FHumidity := aHumidity;
141 Display;
142end;
143
144end.
1
2{《HeadFirst设计模式》之观察者模式 }
3{ 客户端 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :[email protected] }
6
7program pWeatherReport;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uWeatherReport in ‘uWeatherReport.pas‘;
14
15var
16 aWeatherData: TWeatherData;
17 aCurrentConditionsDisplay: TCurrentConditionsDisplay;
18
19begin
20 aWeatherData := TWeatherData.Create;
21 aCurrentConditionsDisplay := TCurrentConditionsDisplay.Create(aWeatherData);
22
23 aWeatherData.SetMeasurements(80, 65, 30);
24
25 FreeAndNil(aWeatherData);
26 FreeAndNil(aCurrentConditionsDisplay);
27
28 Readln;
29end.
运行结果: