DesignPattern之SimpleFactory

ZHANGDENGHUI_

ZHANGDENGHUI_

2016-02-19 19:34

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

  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;

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

  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;

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

  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;
  

展开更多 50%)
分享

猜你喜欢

DesignPattern之SimpleFactory

编程语言 网络编程
DesignPattern之SimpleFactory

DesignPattern之FactoryMethod

编程语言 网络编程
DesignPattern之FactoryMethod

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

甲之蜜糖乙之砒霜

护肤方法
甲之蜜糖乙之砒霜

另类之美之红外摄影技巧

平面设计 海报设计 广告设计 画报设计 签名设计 服装设计 名片设计 画册设计 版式设计 商标设计
另类之美之红外摄影技巧

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

人体之阴阳之气气是什么

中医保健 中医养生 中草药
人体之阴阳之气气是什么

《龙之纪元起源之觉醒》法师心得

电脑网络
《龙之纪元起源之觉醒》法师心得

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

支持XP下托盘栏气球提示的托盘单元

支持XP下托盘栏气球提示的托盘单元

SQL Server 2005中使用DDL触发器监控数据库变化

SQL Server 2005中使用DDL触发器监控数据库变化
下拉加载更多内容 ↓