首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
首页 | JAVA | C# | VB | VB.NET | C/C++ | delphi | 工程管理 | 其他语言 | 论坛
各大城市软件开发培训、软件人才免费咨询热线:400-700-5807
 您现在的位置: 中国IT实验室 >> 桌面开发 >> Delphi >> 正文
用Delphi2005学设计模式之工厂方法篇
来源:ChinaItLab 作者:佚名 时间:2007-4-5

  本文完成以下内容:
  
  1、代码用支持中文的 Delphi 2005 编译并通过,并去除了其中一些无关紧要的地方,如异常处理等 ;
  
  2、重新设计一个情景,分别用“简单工厂模式”和“工厂方法模式”两种方法实现,请体会其中的差别 ;
  
  3、在情景中添加一个子类后,请体会“简单工厂模式”和“工厂方法模式”两种方法不同的处理方式;
  
  4、如果不理解什么是接口、多态、静态函数等概念,这里不作解释,请看第一章或找相关资料;
  
  5、本文的情景和上文差不多,只是把工厂从“果园”变成了“水果小贩”;同样的三种水果:苹果、葡萄、草莓;每种水果都封装了两个逻辑,在和外部“交易”时会用到这两个逻辑。 最后,请重新回顾“开闭原则”
  
  下面开始吧。
  
  这里是简单工厂模式的实现方法,在这种模式中:
  
  1、一个小贩要负责所有三种水果的交易,这对他来说是很大的挑战噢,不信您看!
  
  2、顾客必须对水果的名字有一个准确地描述,这样水果才会卖给你,这很影响生意呀!
  
  //简单工厂类和水果类单元文件
  
  unit SimpleFactory;
  
  interface
  
  type
  接口_水果 = interface(IInterface)
  function 提示():string;
  function 被评价():string;
  end;
  
  类_苹果 = class(TInterfacedObject, 接口_水果)
  function 提示():string;
  function 被评价():string;
  end;
  
  类_葡萄 = class(TInterfacedObject, 接口_水果)
  function 提示():string;
  function 被评价():string;
  end;
  
  类_草莓 = class(TInterfacedObject, 接口_水果)
  function 提示():string;
  function 被评价():string;
  end;
  
  工厂类_小贩 = class(TObject)
  public
  class function 工厂(水果名:string): 接口_水果;
  end;
  
  implementation
  
  {***** 类_苹果 *****}
  
  function 类_苹果.提示():string;
  begin
  result:='削了皮再吃!';
  end;
  
  function 类_苹果.被评价():string;
  begin
  result:='恩,还不错,挺甜!';
  end;
  
  {*****类_葡萄 *****}
  
  function 类_葡萄.提示():string;
  begin
  result:='别把核咽下去了!';
  end;
  
  function 类_葡萄.被评价():string;
  begin
  result:='没有核呀???';
  end;
  
  {***** 类_草莓 *****}
  
  function 类_草莓.提示():string;
  begin
  result:='别怪我没告诉你,很酸!';
  end;
  
  function 类_草莓.被评价():string;
  begin
  result:='试试?哇,牙快酸掉了!';
  end;
  
  {***** 工厂类_小贩 *****}
  
  class function 工厂类_小贩.工厂(水果名:string): 接口_水果;
  begin
  if(水果名='苹果')then result:=类_苹果.Create()
  else if(水果名='葡萄')then result:=类_葡萄.Create()
  else if(水果名='草莓')then result:=类_草莓.Create();
  end;
  end.
  //窗体单元文件
  
  unit MainForm;
  
  interface
  
  uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,SimpleFactory;
  
  type
  TForm1 = class(TForm)
  RadioButton1: TRadioButton;
  RadioButton2: TRadioButton;
  RadioButton3: TRadioButton;
  procedure RadioButton3Click(Sender: TObject);
  procedure RadioButton2Click(Sender: TObject);
  procedure RadioButton1Click(Sender: TObject);
  private
  procedure 交易(水果名:string);
  end;
  
  var
  Form1: TForm1;
  
  implementation
  
  {$R *.dfm}
  
  procedure TForm1.交易(水果名:string);
  var
  我买的水果: 接口_水果;
  begin
  我买的水果:=工厂类_小贩.工厂(水果名);
  ShowMessage(我买的水果.提示);
  ShowMessage(我买的水果.被评价);
  end;
  
  procedure TForm1.RadioButton1Click(Sender: TObject);
  begin
  交易('苹果');
  end;
  
  procedure TForm1.RadioButton2Click(Sender: TObject);
  begin
  交易('葡萄');
  end;
  
  procedure TForm1.RadioButton3Click(Sender: TObject);
  begin
  交易('草莓');
  end;
  end.
  
  这里是工厂方法模式的实现方法,在这种模式中:
  
  1、每一种水果都对应有一个小贩负责,这样他们做起生意来就轻松多了,呵呵!
  
  2、顾客直接和小贩打交道,他知道您要什么,这样就不会因为说不清那讨厌的水果名字而吃不上说水果了。
  
  //工厂类和水果类单元文件
  
  unit FactoryMethod;
  
  interface
  
  type
  接口_水果 = interface(IInterface)
  function 提示():string;
  function 被评价():string;
  end;
  
  类_苹果 = class(TInterfacedObject, 接口_水果)
  function 提示():string;
  function 被评价():string;
  end;
  
  类_葡萄 = class(TInterfacedObject, 接口_水果)
  function 提示():string;
  function 被评价():string;
  end;
  
  类_草莓 = class(TInterfacedObject, 接口_水果)
  function 提示():string;
  function 被评价():string;
  end;
  
  接口_小贩 = interface(IInterface)
  function 工厂(): 接口_水果;
  end;
  
  类_苹果小贩 = class(TInterfacedObject, 接口_小贩)
  function 工厂(): 接口_水果;
  end;
  
  类_葡萄小贩 = class(TInterfacedObject, 接口_小贩)
  function 工厂(): 接口_水果;
  end;
  
  类_草莓小贩 = class(TInterfacedObject, 接口_小贩)
  function 工厂(): 接口_水果;
  end;
  
  implementation
  
  {****** 类_苹果 ******}
  
  function 类_苹果.提示():string;
  begin
  result:='削了皮再吃!';
  end;
  
  function 类_苹果.被评价():string;
  begin
  result:='恩,还不错,挺甜!';
  end;
  
  {****** 类_葡萄 ******}
  
  function 类_葡萄.提示():string;
  begin
  result:='别把核咽下去了!';
  end;
  
  function 类_葡萄.被评价():string;
  begin
  result:='没有核呀???';
  end;
  
  {****** 类_草莓 ******}
  
  function 类_草莓.提示():string;
  begin
  result:='别怪我没告诉你,很酸!';
  end;
  
  function 类_草莓.被评价():string;
  begin
  result:='试试?哇,牙快酸掉了!';
  end;
  
  {***** 类_苹果小贩 *****}
  
  function 类_苹果小贩.工厂(): 接口_水果;
  begin
  result:=类_苹果.Create()
  end;
  
  {***** 类_葡萄小贩 *****}
  
  function 类_葡萄小贩.工厂(): 接口_水果;
  begin
  result:=类_葡萄.Create()
  end;
  
  {***** 类_草莓小贩 *****}
  
  function 类_草莓小贩.工厂(): 接口_水果;
  begin
  result:=类_草莓.Create()
  end;
  end.
  
  //窗体单元文件
  
  unit MainForm;
  
  interface
  
  uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,FactoryMethod;
  
  type
  TForm1 = class(TForm)
  RadioButton1: TRadioButton;
  RadioButton2: TRadioButton;
  RadioButton3: TRadioButton;
  procedure RadioButton3Click(Sender: TObject);
  procedure RadioButton2Click(Sender: TObject);
  procedure RadioButton1Click(Sender: TObject);
  private
  procedure 交易(小贩:接口_小贩);
  end;
  
  var
  Form1: TForm1;
  
  implementation
  
  {$R *.dfm}
  
  procedure TForm1.交易(小贩:接口_小贩);
  var
  我买的水果:接口_水果;
  begin
  我买的水果:=小贩.工厂();
  ShowMessage(我买的水果.提示);
  ShowMessage(我买的水果.被评价);
  end;
  
  procedure TForm1.RadioButton1Click(Sender: TObject);
  begin
  交易(类_苹果小贩.Create);
  end;
  
  procedure TForm1.RadioButton2Click(Sender: TObject);
  begin
  交易(类_葡萄小贩.Create);
  end;
  
  procedure TForm1.RadioButton3Click(Sender: TObject);
  begin
  交
【责编:Lili】
中国IT教育热线咨询
相关文章
Delphi初学者应小心的六大陷阱
基于Delphi的异常处理技术探究
Delphi中用API实现在MSN的信息提示
Delphi中"包"的妙用…
如何用Delphi实现子目录级的文件查询
在Delphi数据库应用程序中常见错误
如何使用Delphi实现无边界窗体的移动
如何用Delphi创建快捷方式
利用 Delphi 轻松编制压缩助理程序…
经验技巧:分享两条Delphi开发经验
推荐文章

 精彩友情推荐
·Asp源码 PHP源码
·CGI源码 JSP源码
·建站书籍教程
·服务器软件 .net源码
·建站工具软件
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
最新更新 推荐文章
·框架:J2EE WEB应用架构分析…03-13
·几种VC++数据库开发技术的相对比…03-13
·利用C#实现标注式消息提示窗口03-13
·用C#创建COM对象03-13
·Visual C#多线程参数传递浅析…03-13
·Visual C#多线程参数传递浅析…03-13
·基于HOOK和MMF的Win密码渗透技术11-15
·Visual C++设计超强仿QQ自动伸缩…11-15
·Java SE 6.0实现高质量桌面集成开…11-15
·史玉柱东山再起幕后高人11-15
·用C#创建COM对象09-06
·IT管理十大失误及其对策08-30
·VC中利用MFC设计绘图程序初步08-23
·JAVA中对象创建和初始化过程08-23
·C语言中的位域的使用08-09
·浅谈Java桌面应用程序开发08-09
·C#的前途如何?08-02
·几种VC++数据库开发技术的相对比较07-12
·用Visual C#实现网络封包监视…07-12
·VB.NET中的TextBox控件详解07-12
·VB.NET实现PC与掌上电脑PPC的双向通信07-05