zoukankan      html  css  js  c++  java
  • Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---单例模式之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{ 客户端                         }
     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.

    运行结果:

     
     
  • 相关阅读:
    Windows系统安装mysql5.7*时mysql服务启动失败的解决方法
    安装MySQL出现 This application requires Visual Studio 2013 x64 Redistributable.Please install the Redistributable then run this installer again
    Fiddler抓包流程
    C#使用NPOI根据模板生成Word文件功能实现
    .NET nhibernate 添加新的表运行报is not mapped的问题
    二进制原码、反码、补码和位运算
    【英语】面试常用语整理
    【检测分割算法整理】
    【Leetcode方法比较】DP/滑窗/前缀和
    【Leetcode】数学系列
  • 原文地址:https://www.cnblogs.com/0x2D-0x22/p/4076337.html
Copyright © 2011-2022 走看看