WEB开发网      婵犻潧鍊婚弲顐︽偟椤栨稓闄勯柦妯侯槸閻庤霉濠婂骸浜剧紒杈ㄥ笚閹峰懘鎮╅崹顐ゆ殸婵炴垶鎸撮崑鎾趁归悩鐑橆棄闁搞劌瀛╃粋宥夘敃閿濆柊锕傛煙鐎涙ê鐏f繝濠冨灴閹啴宕熼鍡╀紘婵炲濮惧Λ鍕叏閳哄懎绀夋繛鎴濈-楠炪垽鎮归崶褍妲婚柛銊ュ缁傚秹鏁撻敓锟� ---闂佹寧娲╅幏锟�
开发学院软件开发Delphi DesignPattern之SimpleFactory 阅读

DesignPattern之SimpleFactory

 2006-02-04 14:01:08 来源:WEB开发网 闂侀潧妫撮幏锟�闂佸憡鍨电换鎰版儍椤掑倵鍋撳☉娆嶄沪缂傚稄鎷�婵犫拃鍛粶闁靛洤娲ㄩ埀顒佺⊕閵囩偟绱為敓锟�闂侀潧妫撮幏锟�  闂佺ǹ绻楀▍鏇㈠极閻愬搫绾ч柕濠忕細閼割亜顪冪€n剙浠ф繛鍫熷灥椤曘儵顢欓悡搴ば�
核心提示: unit Unit2;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Controls, Dialogs, ExtCtrls, ShellAPI, AppEvnts, Forms;type IFruitInterface = inter
 

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Controls,
  Dialogs, ExtCtrls, ShellAPI, AppEvnts, Forms;
type
  IFruitInterface = interface
   ['{D4557157-5241-4116-AA1E-87F86A884EA9}']
   PRocedure grow();
   procedure harvest();
   procedure plant();
  end;

type
  TApple = class(TComponent, IFruitInterface)
  private
   treeAge: Integer;
  public
   procedure grow();
   procedure harvest();
   procedure plant();
  end;

  TStrwaBerry = class(TComponent, IFruitInterface)
  private
   treeAge: Integer;
  public
   procedure grow();
   procedure harvest();
   procedure plant();
  end;
  TGrape = class(TComponent, IFruitInterface)
  private
   treeAge: Integer;
  public
   procedure grow();
   procedure harvest();
   procedure plant();
  end;

  TFruitGarden = class
  public
   class function GetFruit(const strFruit: string): IFruitInterface;
  end;

implementation
//{$R *.dfm}
procedure TApple.grow();
begin
  showmessage('Apple grow');
end;

procedure TApple.harvest();
begin
  showmessage('Apple harvest');
end;

procedure TApple.plant();
begin
  showmessage('Apple plant');
end;

procedure TStrwaBerry.grow();
begin
  showmessage('StrwaBerry plant');
end;

procedure TStrwaBerry.harvest();
begin
  showmessage('StrwaBerry plant');
end;

procedure TStrwaBerry.plant();
begin
  showmessage('StrwaBerry plant');
end;

procedure TGrape.grow();
begin
  showmessage('Grape plant');
end;

procedure TGrape.harvest();
begin
  showmessage('Grape plant');
end;

procedure TGrape.plant();
begin
  showmessage('Grape plant');
end;

class function TFruitGarden.GetFruit(const strFruit: string): IFruitInterface;
begin
  if strFruit = 'Apple' then
   Result := TApple.create(nil)
  else if strFruit = 'StrawBerry' then
   Result := TStrwaBerry.create(nil)
  else if strFruit = 'Grape' then
   Result := TGrape.create(nil)
  else
   raise Exception.Create('Cannot create ' + strFruit);
end;

end.

//test

  AFruitGarden: TFruitGarden;
  AFruit: IFruitInterface;
begin

  AFruitGarden := TFruitGarden.Create();
  try
   AFruit := AFruitGarden.GetFruit(Edit1.Text);
   AFruit.grow();
  except on FruitError: Exception do
    ShowMessage(FruitError.Message);
  end;

  FreeAndNil(AFruitGarden);
end;

Tags:DesignPattern SimpleFactory

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接