用 Delphi 学设计模式(一) 之 简单工厂篇 (原创)[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 wlnh_2004 的 blog

end;

function tgrape.plant: string;
begin
  result:='葡萄已经种好了......';
end;

{ ********** tapple ********** }

function tapple.grow: string;
begin
  result:='苹果正在生长......';
end;

function tapple.harvest: string;
begin
  result:='苹果可以收获了......';
end;

function tapple.plant: string;
begin
  result:='苹果已经种好了......';
end;

{ ********** tstrawberry ********** }

function tstrawberry.grow: string;
begin
  result:='草莓正在生长......';
end;

function tstrawberry.harvest: string;
begin
  result:='草莓可以收获了......';
end;

function tstrawberry.plant: string;
begin
  result:='草莓已经种好了......';
end;

{ ********** tfruitgardener ********** }

class function tfruitgardener.factory(whichfruit:string): ifruit;
begin
  //精髓就是这条语句了 result:= tapple.create()
  //不明白赶紧去复习复习什么是多态性
  if(lowercase(whichfruit)='apple')then result:=tapple.create()
  else if(lowercase(whichfruit)='grape')then result:=tgrape.create()
  else if(lowercase(whichfruit)='strawberry')then result:=tstrawberry.create()

本文关键:用 Delphi 学设计模式(一) 之 简单工厂篇 (原创)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top