1。我的描述不太规范,进货单是一个类,不是说每个进货单都生成一个类
应该改成 把进货单设计成类,不是说每个进货单都生成一个对象,全局只
要几个和进货单相关的表单需要生成一个这样的对象就够了。
2。进货单类由上层的进货管理类管理,查找进货单的由操作进货管理类实现
unit StockManager;
interface
uses
DB, SysUtils, DBTables;
type
TStockDoc = class //进货类
public
procedure Assign(AQuery: TQuery);
virtual;
abstract;
procedure update(SQL: string;
IsOpen: boolean);
virtual;
abstract;
end;
TStockDocument = class;
TStockManager = class //进货单管理类 //
private
FStockDocCount: Integer;
FStockCount: Integer;
FStockDocArray: array of TStockDoc;
//根据需要控制进货单的对象个数
procedure InitStockDocument;
procedure CleanupStockDocument;
function FindDocument(StockDocument: TStockDoc): Integer;
public
constructor Create(StockCount: Integer);
destructor Destroy;
override;
function FindStockDocument(SQL: string;
Query: TQuery): TStockDoc;
procedure CloseStockDocument(StockDocument: TStockDoc);
end;
TStockDocument = class(TStockDoc)
private
FQuery: TQuery;
FManager: TStockManager;
function GetStockID: Integer;
procedure SetStockID(const Value: Integer);
public
property StockID: Integer read GetStockID write SetStockID;
property Manager: TStockManager read FManager;
constructor Create(Manager: TStockManager);
destructor Destroy;
override;
procedure update(SQL: string;
IsOpen: boolean);
override;
property Query: TQuery read FQuery;
procedure Assign(AQuery: TQuery);
override;
end;
implementation
{ TStockManager }
procedure TStockManager.CleanupStockDocument;
var
i: Integer;
begin
for i := Low(FStockDocArray) to High(FStockDocArray)do
FStockDocArray.Free;
end;
procedure TStockManager.CloseStockDocument(StockDocument: TStockDoc);
var
index, i: Integer;
begin
index := FindDocument(StockDocument);
if index < 0 then
raise Exception.Create(' ');
if Index = FStockDocCount - 1 then
begin
Dec(FStockDocCount);
Exit;
end;
for I := index to FStockDocCount - 2do
begin
FStockDocArray[index] := FStockDocArray[index + 1];
end;
Dec(FStockDocCount);
FStockDocArray[FStockDocCount] := StockDocument;
end;
constructor TStockManager.Create(StockCount: Integer);
begin
FStockCount := StockCount;
InitStockDocument;
end;
destructor TStockManager.Destroy;
begin
CleanupStockDocument;
inherited;
end;
function TStockManager.FindDocument(StockDocument: TStockDoc): Integer;
var
i: Integer;
begin
Result := -1;
for i := Low(FStockDocArray) to High(FStockDocArray)do
begin
if FStockDocArray = StockDocument then
begin
Result := i;
break;
end;
end;
end;
function TStockManager.FindStockDocument(
SQL: string;
Query: TQuery): TStockDoc;
begin
Result := nil;
if FStockDocCount > 11 then
Exit;
Query.SQL.Clear;
Query.SQL.Add(SQL);
Query.Close;
try
Query.Open;
except
raise;
end;
Result := FStockDocArray[FStockDocCount];
if Result <> nil then
begin
Result.Assign(Query);
Inc(FStockDocCount);
end;
end;
procedure TStockManager.InitStockDocument;
var
i: Integer;
begin
setlength(FStockDocArray, FStockCount);
for i := Low(FStockDocArray) to High(FStockDocArray)do
begin
FStockDocArray := TStockDocument.Create(Self);
end;
FStockDocCount := 0;
end;
{ TStockDocument }
procedure TStockDocument.Assign(AQuery: TQuery);
begin
FQuery := AQuery;
FQuery.SQL.Clear;
FQuery.SQL.Add('select * from Stock');
FQuery.Close;
try
FQuery.Open;
except
end;
end;
constructor TStockDocument.Create(Manager: TStockManager);
begin
self.FManager := Manager;
end;
destructor TStockDocument.Destroy;
begin
inherited;
end;
function TStockDocument.GetStockID: Integer;
begin
Result := FQuery.FieldByName('StockID').AsInteger;
end;
procedure TStockDocument.SetStockID(const Value: Integer);
begin
if Value <> GetStockID then
FQuery.FieldByName('StockID').AsInteger := Value;
end;
procedure TStockDocument.update(SQL: string;
IsOpen: boolean);
begin
FQuery.SQL.Clear;
FQuery.Close;
FQuery.SQL.Add(SQL);
with FQuerydo
begin
if IsOpen then
FQuery.Open
else
FQuery.ExecSQL;
end
end;
end.
上面是我刚才写的一个简单例子,就是实现我所说的观点。