O
Opportunity
Unregistered / Unconfirmed
GUEST, unregistred user!
做基础参数配置模块时,实现对特定表的“增、删、改”是最基础的功能,几乎每一个配置子单元都会用到。而且,这些功能实现的代码,大同小异。如果就这么“平铺直叙”的,这些“重复”功能的编码实现,几乎是先COPY、PLASTER再做相应的修改。当配置子单元很多时,其工作将是极其枯燥乏味而又漫长的!
如何应用OOP思想(封装、继承、多态),实现代码(接口)重用,化繁为简呢?
以下面的代码为例,看各路高手如何各显神通!
(代码比较长,但逻辑很简单)
unit uMainCfg; {主界面}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActnList, Db, DBTables, Grids, DBGrids, Menus;
type
TFormCfg = class(TForm)
ActionList1: TActionList;
ActAIns: TAction;
ActAUpd: TAction;
ActADel: TAction;
Query: TQuery;
DataSourceA: TDataSource;
QueryA: TQuery;
DBGridA: TDBGrid;
PopupMenu1: TPopupMenu;
ActAIns1: TMenuItem;
ActAUpd1: TMenuItem;
ActADel1: TMenuItem;
procedure ActAInsExecute(Sender: TObject);
procedure ActAUpdExecute(Sender: TObject);
procedure ActADelExecute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormCfg: TFormCfg;
implementation
uses
uDlgA; {实现增、改的配置界面}
{$R *.DFM}
{增加}
procedure TFormCfg.ActAInsExecute(Sender: TObject);
const
Script = 'INSERT INTO TAB_A (ID, NAME, WAGE,DATE) '+#13#10+
' VALUES ID, :NAME, :WAGE, ATE) ';
var
iID: Integer;
begin
inherited;
try
FmDlgA:= TFmDlgA.Create(Self);
with FmDlgA do
begin
iDlgActID := 1; // 增加
// 参数初始化
giID:= GetNextvalTabA;
ShowModal;
if ModalResult= mrOK then begin
with Query do
begin
Close;
Sql.Text:= Script;
iID:= StrToInt(Trim(EditID.Text));
ParamByName('ID').AsInteger:= iID;
ParamByName('NAME').AsString:= Trim(EditName.Text);
ParamByName('WAGE').AsFloat:= StrToFloat(Trim(EditWage.Text));
ParamByName('DATE').AsDateTime:= DateTimePickerDate.DateTime;
try
ExecSql;
Commit; // 数据提交
RefreshTabA(iID); // 刷新配置表
except
on E: Exception do
begin
Rollback; // 数据回退
EMsgBox('增加××时出错!', '错误消息:'+E.Message);
Exit;
end;
end;
end;
end;
end;
finally
FmDlgA.Free;
end;
end;
{修改}
procedure TFormCfg.ActAUpdExecute(Sender: TObject);
const
Script = 'UPDATE TAB_A '+#13#10+
' SET NAME = :NAME, '+#13#10+
' WAGE = :WAGE, '+#13#10+
' DATE = ATE '+#13#10+
' WHERE ID = :ID ';
var
iID: Integer;
begin
inherited;
try
FmDlgA:= TFmDlgA.Create(Self);
with FmDlgA do
begin
iDlgActID := 0; // 修改
// 参数初始化
SetParaTabA(giID, gsName, gfWage, gdDate);
ShowModal;
if ModalResult= mrOK then begin
with Query do
begin
Close;
Sql.Text:= Script;
iID:= QueryA.FieldByName('ID').AsInteger;
ParamByName('ID').AsInteger:= iID;
ParamByName('NAME').AsString:= Trim(EditName.Text);
ParamByName('WAGE').AsFloat:= StrToFloat(Trim(EditWage.Text));
ParamByName('DATE').AsDateTime:= DateTimePickerDate.DateTime;
try
ExecSql;
Commit;
RefreshTabA(iID);
except
on E: Exception do
begin
Rollback;
EMsgBox('修改××时出错!', '错误消息:'+E.Message);
Exit;
end;
end;
end;
end;
end;
finally
FmDlgA.Free;
end;
end;
{删除}
procedure TFormCfg.ActADelExecute(Sender: TObject);
const
Script = 'DELETE FROM TAB_A '+ #13#10 +
' WHERE ID = :ID ';
var
iID: Integer;
begin
inherited;
if Not MsgConfirm('确定要删除该××吗?') then
Exit;
with Query do
begin
Close;
Sql.Text:= Script;
ParamByName('ID').AsInteger:= iID;
try
ExecSql;
Commit;
RefreshTabA(0);
except
on E: Exception do
begin
Rollback;
EMsgBox('删除××时出错!', '错误消息:'+E.Message);
Exit;
end;
end;
end;
end;
end.
/=============================================================================/
unit uDlgA;{实现增、改的配置界面}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;
type
TFmDlgA = class(TForm)
BtnOK: TButton;
BtnCancel: TButton;
EditID: TEdit;
EditName: TEdit;
EditWage: TEdit;
DateTimePickerDate: TDateTimePicker;
procedure BtnOKClick(Sender: TObject);
procedure BtnCancelClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
iDlgActID: Integer;
// 交互的参数
giID: Integer;
gsName: string;
gfWage: Double;
gdDate: TDateTime;
{ Public declarations }
end;
var
FmDlgA: TFmDlgA;
implementation
{$R *.DFM}
procedure TFmDlgA.FormShow(Sender: TObject);
begin
Case iDlgActID of
0:
begin
Caption := '修改××';
EditID.Text:= IntToStr(giID);
EditName.Text:= gsName;
EditWage.Text := FloatToStr(gfWage);
DateTimePickerDate.Date := gdDate;
end;
1:
begin
Caption := '增加××';
EditID.Text:= IntToStr(giID);
end;
end;
EditID.SetFocus;
end;
procedure TFmDlgA.BtnOKClick(Sender: TObject);
begin
if Not CheckValue then //用于数据稽核,核查通过为真
Exit;
ModalResult:= mrOK;
end;
procedure TFmDlgA.BtnCancelClick(Sender: TObject);
begin
ModalResult:= mrCancel;
end;
end.
如何应用OOP思想(封装、继承、多态),实现代码(接口)重用,化繁为简呢?
以下面的代码为例,看各路高手如何各显神通!
(代码比较长,但逻辑很简单)
unit uMainCfg; {主界面}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActnList, Db, DBTables, Grids, DBGrids, Menus;
type
TFormCfg = class(TForm)
ActionList1: TActionList;
ActAIns: TAction;
ActAUpd: TAction;
ActADel: TAction;
Query: TQuery;
DataSourceA: TDataSource;
QueryA: TQuery;
DBGridA: TDBGrid;
PopupMenu1: TPopupMenu;
ActAIns1: TMenuItem;
ActAUpd1: TMenuItem;
ActADel1: TMenuItem;
procedure ActAInsExecute(Sender: TObject);
procedure ActAUpdExecute(Sender: TObject);
procedure ActADelExecute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormCfg: TFormCfg;
implementation
uses
uDlgA; {实现增、改的配置界面}
{$R *.DFM}
{增加}
procedure TFormCfg.ActAInsExecute(Sender: TObject);
const
Script = 'INSERT INTO TAB_A (ID, NAME, WAGE,DATE) '+#13#10+
' VALUES ID, :NAME, :WAGE, ATE) ';
var
iID: Integer;
begin
inherited;
try
FmDlgA:= TFmDlgA.Create(Self);
with FmDlgA do
begin
iDlgActID := 1; // 增加
// 参数初始化
giID:= GetNextvalTabA;
ShowModal;
if ModalResult= mrOK then begin
with Query do
begin
Close;
Sql.Text:= Script;
iID:= StrToInt(Trim(EditID.Text));
ParamByName('ID').AsInteger:= iID;
ParamByName('NAME').AsString:= Trim(EditName.Text);
ParamByName('WAGE').AsFloat:= StrToFloat(Trim(EditWage.Text));
ParamByName('DATE').AsDateTime:= DateTimePickerDate.DateTime;
try
ExecSql;
Commit; // 数据提交
RefreshTabA(iID); // 刷新配置表
except
on E: Exception do
begin
Rollback; // 数据回退
EMsgBox('增加××时出错!', '错误消息:'+E.Message);
Exit;
end;
end;
end;
end;
end;
finally
FmDlgA.Free;
end;
end;
{修改}
procedure TFormCfg.ActAUpdExecute(Sender: TObject);
const
Script = 'UPDATE TAB_A '+#13#10+
' SET NAME = :NAME, '+#13#10+
' WAGE = :WAGE, '+#13#10+
' DATE = ATE '+#13#10+
' WHERE ID = :ID ';
var
iID: Integer;
begin
inherited;
try
FmDlgA:= TFmDlgA.Create(Self);
with FmDlgA do
begin
iDlgActID := 0; // 修改
// 参数初始化
SetParaTabA(giID, gsName, gfWage, gdDate);
ShowModal;
if ModalResult= mrOK then begin
with Query do
begin
Close;
Sql.Text:= Script;
iID:= QueryA.FieldByName('ID').AsInteger;
ParamByName('ID').AsInteger:= iID;
ParamByName('NAME').AsString:= Trim(EditName.Text);
ParamByName('WAGE').AsFloat:= StrToFloat(Trim(EditWage.Text));
ParamByName('DATE').AsDateTime:= DateTimePickerDate.DateTime;
try
ExecSql;
Commit;
RefreshTabA(iID);
except
on E: Exception do
begin
Rollback;
EMsgBox('修改××时出错!', '错误消息:'+E.Message);
Exit;
end;
end;
end;
end;
end;
finally
FmDlgA.Free;
end;
end;
{删除}
procedure TFormCfg.ActADelExecute(Sender: TObject);
const
Script = 'DELETE FROM TAB_A '+ #13#10 +
' WHERE ID = :ID ';
var
iID: Integer;
begin
inherited;
if Not MsgConfirm('确定要删除该××吗?') then
Exit;
with Query do
begin
Close;
Sql.Text:= Script;
ParamByName('ID').AsInteger:= iID;
try
ExecSql;
Commit;
RefreshTabA(0);
except
on E: Exception do
begin
Rollback;
EMsgBox('删除××时出错!', '错误消息:'+E.Message);
Exit;
end;
end;
end;
end;
end.
/=============================================================================/
unit uDlgA;{实现增、改的配置界面}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;
type
TFmDlgA = class(TForm)
BtnOK: TButton;
BtnCancel: TButton;
EditID: TEdit;
EditName: TEdit;
EditWage: TEdit;
DateTimePickerDate: TDateTimePicker;
procedure BtnOKClick(Sender: TObject);
procedure BtnCancelClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
iDlgActID: Integer;
// 交互的参数
giID: Integer;
gsName: string;
gfWage: Double;
gdDate: TDateTime;
{ Public declarations }
end;
var
FmDlgA: TFmDlgA;
implementation
{$R *.DFM}
procedure TFmDlgA.FormShow(Sender: TObject);
begin
Case iDlgActID of
0:
begin
Caption := '修改××';
EditID.Text:= IntToStr(giID);
EditName.Text:= gsName;
EditWage.Text := FloatToStr(gfWage);
DateTimePickerDate.Date := gdDate;
end;
1:
begin
Caption := '增加××';
EditID.Text:= IntToStr(giID);
end;
end;
EditID.SetFocus;
end;
procedure TFmDlgA.BtnOKClick(Sender: TObject);
begin
if Not CheckValue then //用于数据稽核,核查通过为真
Exit;
ModalResult:= mrOK;
end;
procedure TFmDlgA.BtnCancelClick(Sender: TObject);
begin
ModalResult:= mrCancel;
end;
end.