DesignPattern之SimpleFactory

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航