DesignPattern之FactoryMethod

nice囚牢

nice囚牢

2016-02-19 19:34

只要你有一台电脑或者手机,都能关注图老师为大家精心推荐的DesignPattern之FactoryMethod,手机电脑控们准备好了吗?一起看过来吧!
 

  unit Factory;

  interface
  uses Windows, Messages, SysUtils, Controls, Product;

  type
    IFactoryInterface = interface
      function factory(): IProductInterface;
    end;

    TFactory1 = class(TControl, IFactoryInterface)
    public
      function factory(): IProductInterface;
    end;

    TFactory2 = class(TControl, IFactoryInterface)
    public
      function factory(): IProductInterface;
    end;

  implementation

  function TFactory1.factory(): IProductInterface;
  var
    AProduct: TProduct1;
  begin
    AProduct := TProduct1.Create(nil);
    Result := AProduct.CreateProduct();
  end;

  function TFactory2.factory(): IProductInterface;
  var
    AProduct: TProduct2;
  begin
    AProduct := TProduct2.Create(nil);
    Result := AProduct.CreateProduct();
  end;

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

  end.
  

  

  unit Product;

  interface
  uses Windows, Messages, SysUtils, Controls, Dialogs;

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

  type
    IProductInterface = interface
      function CreateProduct(): IProductInterface;
    end;

    TProduct1 = class(TControl, IProductInterface)
    public
      function CreateProduct(): IProductInterface;
    end;
    TProduct2 = class(TControl, IProductInterface)
    public
      function CreateProduct(): IProductInterface;
    end;

  implementation

  function TProduct1.CreateProduct(): IProductInterface;
  begin
    Result := Self;
    showmessage('Product1 Created');
  end;

  function TProduct2.CreateProduct(): IProductInterface;
  begin
    Result := Self;
    showmessage('Product2 Created');
  end;

  
  end.
  

  //测试

  procedure TForm1.Button1Click(Sender: TObject);
  var
    AFactory: TFactory1;
  begin
    AFactory := TFactory1.Create(Self);
    AFactory.factory;
    FreeAndNil(AFactory);
  end;

  procedure TForm1.Button2Click(Sender: TObject);
  var
    AFactory: TFactory2;
  begin
    AFactory := TFactory2.Create(Self);
    AFactory.factory;
    FreeAndNil(AFactory);
  end;
  

  

展开更多 50%)
分享

猜你喜欢

DesignPattern之FactoryMethod

编程语言 网络编程
DesignPattern之FactoryMethod

DesignPattern之SimpleFactory

编程语言 网络编程
DesignPattern之SimpleFactory

s8lol主宰符文怎么配

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

甲之蜜糖乙之砒霜

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

另类之美之红外摄影技巧

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

lol偷钱流符文搭配推荐

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

人体之阴阳之气气是什么

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

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

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

lolAD刺客新符文搭配推荐

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

Delphi控件制作技巧[二]

Delphi控件制作技巧[二]

CSS编辑器:TopStyle Pro使用技巧

CSS编辑器:TopStyle Pro使用技巧
下拉加载更多内容 ↓