设计模式学习心得之二(Factory Method)[1]

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

本文简介:选择自 dhsoft 的 blog

unit unit2

  设计模式学习心得之二(factory method)
<<东辉软件>>陈建荣 qq:68468540 欢迎大家和我交流心得

复习:上回讲了设计模式中的第一大类(对象创建型模式)中的第一小类(抽象工厂).说的再明白一点,实际上就是用一个:

 

    mytest.msg;

 

而做到了用不同子类的不同方法,创建不同类的不同子类的对象.也就是说子类可以是:

 

   tsubclassone.create;

   tsubclasstwo.create;

   tsubclassthree.create;

..............

而调用语句

    mytest.test;

什么都不用改,也不必知道实现的是one,two,还是three,还可以是four,five..........从某种意义上来说,是实现了实例创建的多态.(不知道形容恰不恰当,如有不当请指出,谢谢).

    一般情况下,我都使例子尽可能的简单.

//-------------------------------------------------------------------------------------------

好,复习就这样吧.原来想一个一个地按照顺序地写下来,只是builder的例子想不出简单点的,所以就先跳过吧(以后想个简单点的补上),这次就来讲讲factory method(工厂方法) ;

////////////////////////////////////////////////////////////////////////////////////

unit unit2;

interface
uses windows, dialogs, sysutils, classes;

type
//---------------------------------------------------------------------
tproductid = (one, two, three);
tproduct = class //定义产品父类
public
  constructor create; virtual;
end;
tproductone = class(tproduct) //定义产品一
    constructor create; override;
end;
tproducttwo = class(tproduct) //定义产品二
    constructor create; override;
end;
tproductthree = class(tproduct) //定义产品三
    constructor create; override;
end;
// 当然.还可以是four,five...................
//---------------------------------------------------------------------
tcreator = class //定义创建者 ,实现缺省定义
public
 constructor create; virtual;
    function createproduct(productid: tproductid): tproduct; virtual;
end;

tmycreator = class(tcreator) //定义我的创建者,实现我的定义
 constructor create; override;
    function createproduct(productid: tproductid): tproduct; override;
end;
//----------------------------------------------------------------------
implementation

constructor tcreator.create;
begin
//
end;


function tcreator.createproduct(productid: tproductid): tproduct;
var
  aproduct: tproduct;
begin
  aproduct := nil;
  if productid = one      then     aproduct := tproductone.create;
  if productid = two      then     aproduct := tproducttwo.create;
  result := aproduct;
end;

//------------------------------------------------------------------------
constructor tmycreator.create;
begin
// inherited;

end;

function tmycreator.createproduct(productid: tproductid): tproduct;
var
  aproduct: tproduct;
  acreator: tcreator;
begin
  aproduct := nil;                   //在我的定义中调换one和two;
  if productid = one     then     aproduct := tproducttwo.create;
  if productid = two     then     aproduct := tproductone.create;
  if productid = three   then     aproduct := tproductthree.create;
  if aproduct = nil then             //如果上述对象创建失败,那么就调用父类的工厂方法
 begin
    acreator := tcreator.create;
    aproduct := acreator.createproduct(productid);
 end;
  result := aproduct;
end;
//----------------------------------------------------------------------------


{ tproductone }

constructor tproductone.create;
begin
  showmessage('this is tproductone');
end;

{ tproductthree }

constructor tproductthree.create;
begin
  showmessage('this is tproductthree');
end;

{ tproducttwo }

constructor tproducttwo.create;
begin
  showmessage('this is tproducttwo');
end;

{ tproduct }

constructor tproduct.create;
begin
//
end;

end.

总的来说,工厂方法就是利用给factory对象传递不同的参数,以返回具有相同基类或实现了同一接口的对象。如:

var mytest: tcreator;

mytest := tmycreator.create;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
好了,接下来就改变工厂创建的参数就实现了对象创建时的多态 也就是说用factory不同的子类的不同方法,创建同类的不同子类的对象.
注意:抽象工厂实现了实例创建时的多态; 用factory的的不同的子类的不同方法,创建不同类的不同子类
的对象;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mytest.createproduct(one);

mytest.createproduct(two);

mytest.createproduct(three);

 

这次特奉上源码,方便大家(其实是减少我描述的时间):),

表达能力有限,希望大家能够看懂,具体的还请看<<设计模式>>一书.如果有不当之处,敬请赐教.不要骂得太惨哟:)续.

本文关键:设计模式 FACTORY METHOD 学习心得
  相关方案
Google
 

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

go top