Delphi中实现界面与业务逻辑的分离
J雪(zhuam)ndeveloper@sina.com
在做Delphi软件开发之前,我从事Java软件的开发工作,从Java开源社区我学到了很多软件的设计理想,这也许就是我从Java那里得到的回报啊! 开阔了眼界!
最近的项目是用Delphi开发,所以我又看起了Delphi,一个月的时间里我看了差不多看了4本Delphi方面书籍,在做Delphi项目的时候我更是用DELPHI的语法,JAVA的思想来进行软件的开发与设计,感觉有些累!啊,闲话少说啊,进入正题吧!
DELPHI是一个快速软件开发的IDE,通常的Programmer 都是先画View(界面) ,然后在在相应的事件里面书写Source Code,看事例:
1、比如我要向数据库中插入一条记录,通常的做法是这样吧!
SQL Example: Insert Into ExampleTable1 (Field1,Field2,Field3) Values(Values1,Values2,Values3)
现在假设这个DELPHI窗体上有三个TEXT控件,Name分别为 Frist,Second,Three
下面我用三种不同方法将数据插入到数据库中:
1、直接插入
client ----------> Database
Insert Into ExampleTable1 (Field1,Field2,Field3) Values(Frist.text,Second.text,Three.text)
2、间接插入
client ---(Text传递)---> dataClass -------> Database
意思是先将该窗体数据保存到一个数据类中去,然后在由用户从这个数据类中取数据,将这些数据
传到数据库中去
注意:
窗体控件是直接通过TEXT将数据存储到(dataClass)数据类中去的。
这个dataClass只是用于存储数据状态的,里面全是属性,没有业务逻辑的实现!
如下:
{---------------------------------------------
author:zhuam
date:2004/09/04
type:class
property:all AssociatorRunBean Information Set Mothed
descripte: 用于保存会员的行驶证信息 ,
-----------------------------------------------}
type
TAssociatorRunBean=class(TObject)
private
FKiloMetre: Double;
FCarNumber: string;
FNumber17: string;
FCarColor: string;
FAssociatorID: string;
FCarCapacity: string;
FFrameNumber: string;
FEngineNumber: string;
FAvailabilityDate: TDate;
FRegisterDate: TDate;
FBackPicture:TImage;
FFrontPicture: TImage;
FLeftPicture: TImage;
FRightPicture: TImage;
function getBackPicture: TImage;
function getFrontPicture: TImage;
function getLeftPicture: TImage;
function getRightPicture: TImage;
procedure setAssociatorID(const Value: string);
procedure setAvailabilityDate(const Value: TDate);
procedure setBackPicture(const Value: TImage);
procedure setCarCapacity(const Value: string);
procedure setCarColor(const Value: string);
procedure setCarNumber(const Value: string);
procedure setEngineNumber(const Value: string);
procedure setFrameNumber(const Value: string);
procedure setFrontPicture(const Value: TImage);
procedure setKiloMetre(const Value: Double);
procedure setLeftPicture(const Value: TImage);
procedure setNumber17(const Value: string);
procedure setRegisterDate(const Value: TDate);
procedure setRightPicture(const Value: TImage);
public
constructor create;
destructor destroy;override;
property AssociatorID:string read FAssociatorID write setAssociatorID; //会员号码
property CarNumber:string read FCarNumber write setCarNumber; //车牌号码
property CarColor:string read FCarColor write setCarColor; //汽车颜色
property CarMode:string read FCarColor write setCarColor; //车型
property EngineNumber:string read FEngineNumber write setEngineNumber; //发动机号码
property FrameNumber:string read FFrameNumber write setFrameNumber; //车架号
property CarCapacity:string read FCarCapacity write setCarCapacity; //排量
property Number17:string read FNumber17 write setNumber17; //17位号
property KiloMetre:Double read FKiloMetre write setKiloMetre; //公里数
property RegisterDate:TDate read FRegisterDate write setRegisterDate; //注册日期
property AvailabilityDate:TDate read FAvailabilityDate write setAvailabilityDate; //有效日期
property FrontPicture:TImage read getFrontPicture write setFrontPicture;
property BackPicture:TImage read getBackPicture write setBackPicture;
property LeftPicture:TImage read getLeftPicture write setLeftPicture;
property RightPicture:TImage read getRightPicture write setRightPicture;
end;
Insert Into ExampleTable1 (Field1,Field2,Field3) Values(AssociatorRunBean.Frist,AssociatorRunBean.Second,AssociatorRunBean.text)
3、间接插入
client ---(自定义property传递)---> dataClass -------> Database
意思是先将该窗体数据保存到一个数据类中去,然后在由用户从这个数据类中取数据,将这些数据
传到数据库中去
注意:
窗体控件是直接通过的自定义property将数据存储到(dataClass)数据类中去的。
这个dataClass只是用于存储数据状态的,里面全是属性,没有业务逻辑的实现!
Insert Into ExampleTable1 (Field1,Field2,Field3) Values(AssociatorRunBean.Frist,AssociatorRunBean.Second,AssociatorRunBean.text)
说到这里有人会问我,这样实现有什么意义哩!细心的同志也许已经有所察觉啊!
这正是完成Delphi界面与业务逻辑的分离的一种手段啊