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;