码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---单例模式之ChocolateBoiler[转]

时间:2014-11-05 16:24:50      阅读:282      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》之单例模式 }
 3bubuko.com,布布扣{ 编译工具: Delphi2007 for win32 }
 4bubuko.com,布布扣{ E-Mail : guzh-0417@163.com  }
 5bubuko.com,布布扣
 6bubuko.com,布布扣unit uChocolateBoiler;
 7bubuko.com,布布扣
 8bubuko.com,布布扣interface
 9bubuko.com,布布扣
10bubuko.com,布布扣type
11bubuko.com,布布扣  TChocolateBoiler = class(TObject)
12bubuko.com,布布扣  strict private
13bubuko.com,布布扣    class var
14bubuko.com,布布扣      FUniqueInstance: TChocolateBoiler;
15bubuko.com,布布扣  strict private
16bubuko.com,布布扣    FEmpty : Boolean;
17bubuko.com,布布扣    FBoiled: Boolean;
18bubuko.com,布布扣    constructor Create;
19bubuko.com,布布扣  public
20bubuko.com,布布扣    class function GetInstance: TChocolateBoiler;
21bubuko.com,布布扣    function IsEmpty : Boolean;
22bubuko.com,布布扣    function IsBoiled: Boolean;
23bubuko.com,布布扣    procedure Fill;
24bubuko.com,布布扣    procedure Drain;
25bubuko.com,布布扣    procedure Boil;
26bubuko.com,布布扣  end;
27bubuko.com,布布扣
28bubuko.com,布布扣implementation
29bubuko.com,布布扣
30bubuko.com,布布扣{ TChocolateBoiler }
31bubuko.com,布布扣
32bubuko.com,布布扣procedure TChocolateBoiler.Boil;
33bubuko.com,布布扣begin
34bubuko.com,布布扣  if (not IsEmpty) and (not IsBoiled) then
35bubuko.com,布布扣    FBoiled := True;
36bubuko.com,布布扣end;
37bubuko.com,布布扣
38bubuko.com,布布扣constructor TChocolateBoiler.Create;
39bubuko.com,布布扣begin
40bubuko.com,布布扣  FEmpty  := True;
41bubuko.com,布布扣  FBoiled := False;
42bubuko.com,布布扣end;
43bubuko.com,布布扣
44bubuko.com,布布扣procedure TChocolateBoiler.Drain;
45bubuko.com,布布扣begin
46bubuko.com,布布扣  if (not IsEmpty) and IsBoiled then
47bubuko.com,布布扣    FEmpty := True;
48bubuko.com,布布扣end;
49bubuko.com,布布扣
50bubuko.com,布布扣procedure TChocolateBoiler.Fill;
51bubuko.com,布布扣begin
52bubuko.com,布布扣  if IsEmpty then
53bubuko.com,布布扣  begin
54bubuko.com,布布扣    FEmpty  := False;
55bubuko.com,布布扣    FBoiled := False;
56bubuko.com,布布扣  end;
57bubuko.com,布布扣end;
58bubuko.com,布布扣
59bubuko.com,布布扣class function TChocolateBoiler.GetInstance: TChocolateBoiler;
60bubuko.com,布布扣begin
61bubuko.com,布布扣  if FUniqueInstance = nil then
62bubuko.com,布布扣  begin
63bubuko.com,布布扣    Writeln(‘Creating unique instance of Chocolate Boiler.‘);
64bubuko.com,布布扣    FUniqueInstance := TChocolateBoiler.Create;
65bubuko.com,布布扣  end;
66bubuko.com,布布扣
67bubuko.com,布布扣  Writeln(‘Returning instance of Chocolate Boiler.‘);
68bubuko.com,布布扣  Result := FUniqueInstance;
69bubuko.com,布布扣end;
70bubuko.com,布布扣
71bubuko.com,布布扣function TChocolateBoiler.IsBoiled: Boolean;
72bubuko.com,布布扣begin
73bubuko.com,布布扣  Result := FBoiled;
74bubuko.com,布布扣end;
75bubuko.com,布布扣
76bubuko.com,布布扣function TChocolateBoiler.IsEmpty: Boolean;
77bubuko.com,布布扣begin
78bubuko.com,布布扣  Result := FEmpty;
79bubuko.com,布布扣end;
80bubuko.com,布布扣
81bubuko.com,布布扣end.

bubuko.com,布布扣
 1bubuko.com,布布扣
 2bubuko.com,布布扣{《HeadFirst设计模式》之单例模式   }
 3bubuko.com,布布扣{ 客户端                         }
 4bubuko.com,布布扣{ 编译工具: Delphi2007 for win32 }
 5bubuko.com,布布扣{ E-Mail : guzh-0417@163.com    }
 6bubuko.com,布布扣
 7bubuko.com,布布扣program pChocolateBoilerController;
 8bubuko.com,布布扣
 9bubuko.com,布布扣{$APPTYPE CONSOLE}
10bubuko.com,布布扣
11bubuko.com,布布扣uses
12bubuko.com,布布扣  SysUtils,
13bubuko.com,布布扣  uChocolateBoiler in ‘uChocolateBoiler.pas‘;
14bubuko.com,布布扣
15bubuko.com,布布扣var
16bubuko.com,布布扣  aBoiler : TChocolateBoiler;
17bubuko.com,布布扣  aBoiler2: TChocolateBoiler;
18bubuko.com,布布扣
19bubuko.com,布布扣begin
20bubuko.com,布布扣  aBoiler := TChocolateBoiler.GetInstance;
21bubuko.com,布布扣  aBoiler.Fill;
22bubuko.com,布布扣  aBoiler.Boil;
23bubuko.com,布布扣  aBoiler.Drain;
24bubuko.com,布布扣
25bubuko.com,布布扣  { will return the existing instance: aBoiler }
26bubuko.com,布布扣  aBoiler2 := TChocolateBoiler.GetInstance;
27bubuko.com,布布扣
28bubuko.com,布布扣  FreeAndNil(aBoiler);
29bubuko.com,布布扣  { FreeAndNil(aBoiler2); 同一对象(aBoiler)不能释放两次。}
30bubuko.com,布布扣
31bubuko.com,布布扣  Readln;
32bubuko.com,布布扣end.

运行结果:

bubuko.com,布布扣

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---单例模式之ChocolateBoiler[转]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!