Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---观察者模式之WeatherReport[转]

2020-12-13 15:38

阅读:570

标签:des   blog   http   io   ar   os   for   sp   div   

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

    

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》之观察者模式 }
 3soscw.com,搜素材{ 客户端                         }
 4soscw.com,搜素材{ 编译工具 :Delphi7.0            }
 5soscw.com,搜素材{ 联系方式 :guzh-0417@163.com    }
 6soscw.com,搜素材
 7soscw.com,搜素材program pWeatherReport;
 8soscw.com,搜素材
 9soscw.com,搜素材{$APPTYPE CONSOLE}
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  SysUtils,
13soscw.com,搜素材  uWeatherReport in ‘uWeatherReport.pas‘;
14soscw.com,搜素材
15soscw.com,搜素材var
16soscw.com,搜素材  aWeatherData: TWeatherData;
17soscw.com,搜素材  aCurrentConditionsDisplay: TCurrentConditionsDisplay;
18soscw.com,搜素材  
19soscw.com,搜素材begin
20soscw.com,搜素材  aWeatherData := TWeatherData.Create;
21soscw.com,搜素材  aCurrentConditionsDisplay := TCurrentConditionsDisplay.Create(aWeatherData);
22soscw.com,搜素材
23soscw.com,搜素材  aWeatherData.SetMeasurements(80, 65, 30);
24soscw.com,搜素材
25soscw.com,搜素材  FreeAndNil(aWeatherData);
26soscw.com,搜素材  FreeAndNil(aCurrentConditionsDisplay);
27soscw.com,搜素材
28soscw.com,搜素材  Readln;
29soscw.com,搜素材end.

 

运行结果:

soscw.com,搜素材

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---观察者模式之WeatherReport[转]

标签:des   blog   http   io   ar   os   for   sp   div   

原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076323.html

上一篇:7.5 网站测试

下一篇:1.6 CSS的层叠特性


评论


亲,登录后才可以留言!