DesignPattern之SimpleFactory

2016-02-19 19:34 0 1 收藏

今天图老师小编给大家精心推荐个DesignPattern之SimpleFactory教程,一起来看看过程究竟如何进行吧!喜欢还请点个赞哦~

【 tulaoshi.com - 编程语言 】

 

  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;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  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;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  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;
  

来源:https://www.tulaoshi.com/n/20160219/1621341.html

延伸阅读
标签: 网络游戏
《巫师之怒》游戏种族介绍之 亡灵 亡灵 异端者 秘法师 博学者 神秘者 亡灵一族曾经是人类,他们甚至是萨诺特大陆上最古老的国家之一,那时他们的文明叫做“泽姆”。他们个子高挑,身材苗条,拥有黑色的皮肤,褐色的眼睛。而现在的亡灵一族,已经腐烂的身体部分完全被机械所替代,他们强大的力量是帝国重要的一环。亡灵一族被大...
《龙之纪元起源之觉醒》人物篇 Awakening觉醒是Dragon Age: Origins的第一个大型扩充片,在2010年3月16日推出,同时有碟版及下载版。之前的也只算是小型的DLC好像The Stone Prisoner、Wardens Keep和Return to Ostagar。这一个扩充片除了新地图,新怪物,还有新同伴,二十四个新魔法,三十二个新技能,六个新的专职,等级上限升到三十五,也有一...
标签: 养生 健康
阳春四月,正是www.Tulaoshi.com春茶上市好时节。外出踏青,怎能少得了茶香相伴? 传统的茶是用来“泡”的,而如今新式的茶则还可以入菜,称为“茶肴”。因有着清新的香味,“茶肴”甫一推出便颇受追求健康的人的喜爱。据了解,位于天河区珠江新城兴安路3号富力丽思卡尔顿酒店内的丽轩餐厅,它的茶肴淡雅而不霸道,清香而不俗气。 ...
香蕉素来有“快乐水果”之称,荷兰科学家研究认为:最符合营养标准又能为人脸上增添笑容的水果是香蕉。它所含有的泛酸等成分,是人体的“开心激素”,能减轻心理压力、排解紧张、提高注意力、解除忧郁,令人快乐开心。现代研究发现,常吃香蕉更能预防12种常见病。 1、预防心血管疾病。当体内缺钾时,会发生心律不齐...
《黑暗之魂2 原罪学者》刃之戒指和金石之誓攻略 今天为大家带来的是《黑暗之魂2:原罪学者》刃之戒指和金石之誓正确用法攻略,一起来看看吧! 刃之戒指和金石之誓用法: 1、wiki的资料是物理攻击+50,物理防御+75,占用四个格子 2、很多人都说这法术不值,确实,如果是对一般武器的话效果不是太明显,但是游戏中有少数多段攻击的武器。如雅...

经验教程

870

收藏

10
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部