DesignPattern之FactoryMethod

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

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

【 tulaoshi.com - 编程语言 】

 

  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;

  end.
  

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

  

  unit Product;

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

  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;

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

  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;
  

  

来源:http://www.tulaoshi.com/n/20160219/1621348.html

延伸阅读
标签: 养生 健康
阳春四月,正是www.Tulaoshi.com春茶上市好时节。外出踏青,怎能少得了茶香相伴? 传统的茶是用来“泡”的,而如今新式的茶则还可以入菜,称为“茶肴”。因有着清新的香味,“茶肴”甫一推出便颇受追求健康的人的喜爱。据了解,位于天河区珠江新城兴安路3号富力丽思卡尔顿酒店内的丽轩餐厅,它的茶肴淡雅而不霸道,清香而不俗气。 ...
标签: 网络游戏
《巫师之怒》游戏种族介绍之 亡灵 亡灵 异端者 秘法师 博学者 神秘者 亡灵一族曾经是人类,他们甚至是萨诺特大陆上最古老的国家之一,那时他们的文明叫做“泽姆”。他们个子高挑,身材苗条,拥有黑色的皮肤,褐色的眼睛。而现在的亡灵一族,已经腐烂的身体部分完全被机械所替代,他们强大的力量是帝国重要的一环。亡灵一族被大...
标签: 网络游戏
《巫师之怒》游戏种族介绍之 精灵 精灵 圣殿武士 牧师 奥法师 恶魔学着 精灵是萨诺特最古老的种族,精灵族最早由各个不同血缘的家族构成。他们的社会结构,就是一个各家族间的大联盟。在经历了“大灾变”年代之后,精灵的人口数量急剧减少,只剩八个大的精灵家族流传了下来,以致于他们都无法再称呼自己为“伟大的”种族。于是...
《黑暗之魂2 原罪学者》刃之戒指和金石之誓攻略 今天为大家带来的是《黑暗之魂2:原罪学者》刃之戒指和金石之誓正确用法攻略,一起来看看吧! 刃之戒指和金石之誓用法: 1、wiki的资料是物理攻击+50,物理防御+75,占用四个格子 2、很多人都说这法术不值,确实,如果是对一般武器的话效果不是太明显,但是游戏中有少数多段攻击的武器。如雅...
标签: 营养价值
镇江的醋,享誉海外。据中国国务院2006年公布的消息,镇江恒顺香醋酿制技艺已被列入首批国家级非物质文化遗产名录,这也是江苏省食品制造业中唯一入选的传统手工技艺。 镇江香醋具有"色、香、酸、醇、浓"的特点,"酸而不涩,香而微甜,色浓味鲜",多次获得国内外的嘉奖。存放时间越久,口味越香醇。这是因为它具有得天独厚的地理环境...

经验教程

668

收藏

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