Delphi

本类阅读TOP10

·游戏外挂设计技术探讨①
·如何使用Delphi设计强大的服务器程序
·分布式网络考试系统原型分析及实现
·用DLL方式封装MDI子窗体。
·使用HOOK随心监视Windows
·Delphi 水晶报表打包解决
·url编码与解码工具附代码
·工人线程中关闭窗体的实现
·hdsi2.0 sql注入部分抓包分析语句
·Borland Delphi 2005 下载

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
简单工厂模式(Simple Factory Pattern)Delphi代码

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    昨天下午,看完 guoyan19811021 的设计模式之简单工厂模式(Simple Factory Pattern)(参见http://www.csdn.net/Develop/read_article.asp?id=26635  之后,为了加深印象,我把上面的VB.Net改成了Delphi,未必有很大的意义,但还是贴出来,与大家共享。

 首先建一个基类 CName,如下 

 type
  CName= Class
    private
     { Public declarations }
    public
     { protected declarations}
     function getFirstName:string;
     function getLastName:string;
    protected
     { Private declarations }
      m_sFirstName:string;
      m_sLastName:string;
  end;

下面是2个继承类

type
  CFirst001= Class(CName)
    public
     { Public declarations }
     constructor Create(a_sInput:string);
    protected
     { protected declarations}
    private
     { Private declarations }
    end;

type
  CFirst002 = Class(CName)
    public
     { Public declarations }
     constructor Create(a_sInput:string);
    protected
     { protected declarations}
    private
     { Private declarations }
    end;

简单工厂类

type
  CNameFactory = Class
   public
     { Public declarations }
     function getName(a_sInput:string):CName;
   protected
     { Protected declarations }
   private
     { Private declarations }
  end;

以下是各个函数的实现部分

  function CName.getFirstName:string;
Begin
  result := m_sFirstName;
End;

function CName.getLastName:string;
Begin
  result := m_sLastName;
End;

constructor CFirst001.Create(a_sInput:string);
var i:integer;
Begin
  inherited Create;
  //Add code here
  i := Pos(' ',a_sInput);
  if(i > 0) then begin //can find the ' '(space)
       m_sFirstName := Copy(a_sInput,1,i - 1);
       m_sFirstName := Trim(m_sFirstName);

       m_sLastName := Copy(a_sInput,i + 1,Length(a_sInput) - i);
       m_sLastName := Trim(m_sLastName);
       end
  else begin   //can't  find the ' '(space)
       m_sFirstName := '';
       m_sLastName := a_sInput;
       end;

End;

 


constructor CFirst002.Create(a_sInput:string);
var
   i,j:integer;
Begin
  inherited Create;
 //Add code here
  i := Pos(',',a_sInput);
  if(i > 0) then begin //can find the ' '(space)
       m_sFirstName := Copy(a_sInput,1,i - 1);
       m_sFirstName := Trim(m_sFirstName);

       m_sLastName := Copy(a_sInput,i + 1,Length(a_sInput) - i);
       m_sLastName := Trim(m_sLastName);
       end
  else begin   //can't  find the ' '(space)
       m_sFirstName := '';
       m_sLastName := a_sInput;
       end;


End;

 

function CNameFactory.getName(a_sInput:string):CName;
var i:integer;
Begin
   i := Pos(',',a_sInput);
   if(i > 0) then
   begin
    result := CFirst002.Create(a_sInput);
   end
   else begin
    result := CFirst001.Create(a_sInput);
   end;
End;

  当然,仅仅这样是不能够运行的,还需要在interface 部分加上uses
Sysutils;因为string操作的部分函数用到了它。

  这样就完成了。  

  如有什么错误,请与联系 




相关文章

相关软件




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有