Delphi中實現界面與業務邏輯的分離
J雪(zhuam)[email protected]
在做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界面與業務邏輯的分離的一種手段啊
新聞熱點
疑難解答
圖片精選