范例程序如下:
Form中有一TTable控件Table1,一TAdoTable控件AdoTable1。使用这两个控件仅仅为了
方便设置数据库连接,在设计期把Table1.DatabaseName, Table.TableName,
TAdoTable.connectionstring与TAdoTable.Table设好。
另外,有一个Datasource1和DBGrid1用于显示数据。
超类定义为:
TGatewayType = (gwTable, gwAdoTable);
TDataGateway = class
private
_Table : TDataSet;
function GetActive: Boolean;
procedure SetActive(const Value: Boolean);
function GetDataSet: TDataSet;
public
property DataSet:TDataSet read GetDataSet;
//TDataSet 中的方法 (仅保留常用的,其他复杂方法可限制在本类内使用,
//本类仅提供业务接口)
procedure Append;
procedure AppendRecord(const Values: array of const);
procedure Cancel;
procedure Close;
procedure Delete;
procedure Edit;
procedure EnableControls;
function FieldByName(const FieldName: string): TField;
procedure First;
procedure Insert;
procedure InsertRecord(const Values: array of const);
function IsEmpty: Boolean;
procedure Last;
procedure Next;
procedure Open;
procedure Post;
procedure Refresh;
function Bof: Boolean;
function Eof: Boolean;
property Active: Boolean read GetActive write SetActive;
//业务方法
//子类虚方法
procedure SetDatabase(DatabaseIdentifier:string); virtual; abstract;
procedure SetTable(TableIdentifier:string); virtual; abstract;
end;
其中(仅举例,其他类似)
procedure TDataGateway.Append;
begin
_Table.Append;
end;
procedure TDataGateway.AppendRecord(const Values: array of const);
begin
_Table.AppendRecord(Values);
end;
function TDataGateway.Bof: Boolean;
begin
Result := _Table.Bof;
end;
TTable子类:
定义:
TTableGW = class(TDataGateway)
public
constructor Create; overload;
destructor Destory; overload;
procedure SetDatabase(DatabaseIdentifier:string); override;
procedure SetTable(TableIdentifier:string); override;
end;
实施:
constructor TTableGW.Create;
begin
inherited;
_Table := TTable.Create(Application);
end;
destructor TTableGW.Destory;
begin
FreeAndNil(_Table);
inherited;
end;
procedure TTableGW.SetDatabase(DatabaseIdentifier: string);
begin
(_Table as TTable).DatabaseName := DatabaseIdentifier;
end;
procedure TTableGW.SetTable(TableIdentifier: string);
begin
(_Table as TTable).TableName := TableIdentifier;
end;
AdoTable子类:
定义:
TAdoTableGW = class(TDataGateway)
public
constructor Create; overload;
destructor Destory; overload;
procedure SetDatabase(DatabaseIdentifier:string); override;
procedure SetTable(TableIdentifier:string); override;
end;
实施:
constructor TAdoTableGW.Create;
begin
inherited;
_Table := TADOTable.Create(Application);
end;
destructor TAdoTableGW.Destory;
begin
FreeAndNil(_Table);
inherited;
end;
procedure TAdoTableGW.SetDatabase(DatabaseIdentifier: string);
begin
(_Table as TADOTable).ConnectionString := DatabaseIdentifier;
end;
procedure TAdoTableGW.SetTable(TableIdentifier: string);
begin
(_Table as TADOTable).TableName := TableIdentifier;
end;
于是,在使用时:
procedure TForm1.FormCreate(Sender: TObject);
begin
TableGW := TADOTableGW.Create;
//若是用BDE表,则 TableGW := TTableGW.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//变动数据源类型时,数据操作代码不用修改。但到数据库的连接因为Ado使用连接字,
//BDE使用数据库名,因此这个差别是必须注意的。
TableGW.SetDatabase(self.ADOTable1.ConnectionString);
TableGW.SetTable(self.ADOTable1.TableName);
//BDE表时:
//TableGW.SetDatabase(self.Table1.DatabaseName);
//TableGW.SetTable(self.Table1.TableName);
//以下的数据库操作不用改变。
self.DataSource1.DataSet := TableGW.DataSet;
TableGW.Active := true;//在DBGrid中显示数据表内容。
end;