Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---单例模式之ChocolateBoiler
Posted on 2009-07-30 20:33 都市牧羊 阅读(827) 评论(3) 编辑 收藏 举报ChocolateBoiler
1
2{《HeadFirst设计模式》之单例模式 }
3{ 编译工具: Delphi2007 for win32 }
4{ E-Mail : guzh-0417@163.com }
5
6unit uChocolateBoiler;
7
8interface
9
10type
11 TChocolateBoiler = class(TObject)
12 strict private
13 class var
14 FUniqueInstance: TChocolateBoiler;
15 strict private
16 FEmpty : Boolean;
17 FBoiled: Boolean;
18 constructor Create;
19 public
20 class function GetInstance: TChocolateBoiler;
21 function IsEmpty : Boolean;
22 function IsBoiled: Boolean;
23 procedure Fill;
24 procedure Drain;
25 procedure Boil;
26 end;
27
28implementation
29
30{ TChocolateBoiler }
31
32procedure TChocolateBoiler.Boil;
33begin
34 if (not IsEmpty) and (not IsBoiled) then
35 FBoiled := True;
36end;
37
38constructor TChocolateBoiler.Create;
39begin
40 FEmpty := True;
41 FBoiled := False;
42end;
43
44procedure TChocolateBoiler.Drain;
45begin
46 if (not IsEmpty) and IsBoiled then
47 FEmpty := True;
48end;
49
50procedure TChocolateBoiler.Fill;
51begin
52 if IsEmpty then
53 begin
54 FEmpty := False;
55 FBoiled := False;
56 end;
57end;
58
59class function TChocolateBoiler.GetInstance: TChocolateBoiler;
60begin
61 if FUniqueInstance = nil then
62 begin
63 Writeln('Creating unique instance of Chocolate Boiler.');
64 FUniqueInstance := TChocolateBoiler.Create;
65 end;
66
67 Writeln('Returning instance of Chocolate Boiler.');
68 Result := FUniqueInstance;
69end;
70
71function TChocolateBoiler.IsBoiled: Boolean;
72begin
73 Result := FBoiled;
74end;
75
76function TChocolateBoiler.IsEmpty: Boolean;
77begin
78 Result := FEmpty;
79end;
80
81end.
1
2{《HeadFirst设计模式》之单例模式 }
3{ 编译工具: Delphi2007 for win32 }
4{ E-Mail : guzh-0417@163.com }
5
6unit uChocolateBoiler;
7
8interface
9
10type
11 TChocolateBoiler = class(TObject)
12 strict private
13 class var
14 FUniqueInstance: TChocolateBoiler;
15 strict private
16 FEmpty : Boolean;
17 FBoiled: Boolean;
18 constructor Create;
19 public
20 class function GetInstance: TChocolateBoiler;
21 function IsEmpty : Boolean;
22 function IsBoiled: Boolean;
23 procedure Fill;
24 procedure Drain;
25 procedure Boil;
26 end;
27
28implementation
29
30{ TChocolateBoiler }
31
32procedure TChocolateBoiler.Boil;
33begin
34 if (not IsEmpty) and (not IsBoiled) then
35 FBoiled := True;
36end;
37
38constructor TChocolateBoiler.Create;
39begin
40 FEmpty := True;
41 FBoiled := False;
42end;
43
44procedure TChocolateBoiler.Drain;
45begin
46 if (not IsEmpty) and IsBoiled then
47 FEmpty := True;
48end;
49
50procedure TChocolateBoiler.Fill;
51begin
52 if IsEmpty then
53 begin
54 FEmpty := False;
55 FBoiled := False;
56 end;
57end;
58
59class function TChocolateBoiler.GetInstance: TChocolateBoiler;
60begin
61 if FUniqueInstance = nil then
62 begin
63 Writeln('Creating unique instance of Chocolate Boiler.');
64 FUniqueInstance := TChocolateBoiler.Create;
65 end;
66
67 Writeln('Returning instance of Chocolate Boiler.');
68 Result := FUniqueInstance;
69end;
70
71function TChocolateBoiler.IsBoiled: Boolean;
72begin
73 Result := FBoiled;
74end;
75
76function TChocolateBoiler.IsEmpty: Boolean;
77begin
78 Result := FEmpty;
79end;
80
81end.
客户端
1
2{《HeadFirst设计模式》之单例模式 }
3{ 客户端 }
4{ 编译工具: Delphi2007 for win32 }
5{ E-Mail : guzh-0417@163.com }
6
7program pChocolateBoilerController;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uChocolateBoiler in 'uChocolateBoiler.pas';
14
15var
16 aBoiler : TChocolateBoiler;
17 aBoiler2: TChocolateBoiler;
18
19begin
20 aBoiler := TChocolateBoiler.GetInstance;
21 aBoiler.Fill;
22 aBoiler.Boil;
23 aBoiler.Drain;
24
25 { will return the existing instance: aBoiler }
26 aBoiler2 := TChocolateBoiler.GetInstance;
27
28 FreeAndNil(aBoiler);
29 { FreeAndNil(aBoiler2); 同一对象(aBoiler)不能释放两次。}
30
31 Readln;
32end.
1
2{《HeadFirst设计模式》之单例模式 }
3{ 客户端 }
4{ 编译工具: Delphi2007 for win32 }
5{ E-Mail : guzh-0417@163.com }
6
7program pChocolateBoilerController;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uChocolateBoiler in 'uChocolateBoiler.pas';
14
15var
16 aBoiler : TChocolateBoiler;
17 aBoiler2: TChocolateBoiler;
18
19begin
20 aBoiler := TChocolateBoiler.GetInstance;
21 aBoiler.Fill;
22 aBoiler.Boil;
23 aBoiler.Drain;
24
25 { will return the existing instance: aBoiler }
26 aBoiler2 := TChocolateBoiler.GetInstance;
27
28 FreeAndNil(aBoiler);
29 { FreeAndNil(aBoiler2); 同一对象(aBoiler)不能释放两次。}
30
31 Readln;
32end.
运行结果: