简单的设计模式,懂设计模式的进来啊 (100分)

  • 主题发起人 主题发起人 土土
  • 开始时间 开始时间

土土

Unregistered / Unconfirmed
GUEST, unregistred user!
1:
TPJD = class
id : string;
name : string;
end;

TPJDList= class(TList)
end;
//这个类的对象可以保存多条pjd记录
2:TPJD= class
id :string;
name ;
string;
end;
TPJDData= class
public
list : TList;
end;
//list 可以保存多条pjd记录
哪位解析下两者分别是用了什么设计模式,是否可以互换的,
 
什么叫设计模式?
 
这里有用设计模式吗?只是普通的封装啊,把存储数据变成存储对象指针
 
这是什么东西?
 
tlist 没用过
TPJD = class
id : string;
name : string;
end;
直接这样就可以了,然后用tlist就行了,相当于连表,不用总是创建那么多的类吧
 
??没看懂什么意思
 
实在是看不出有什么模式在里面,如果硬要往上面套的话,只能说第二种有那么一点迭代的意思。
就我的观点,这两种方法是不能互换的。
第一种使用的是继承,这意味着TList的public方法对客户类是开放,如果你的客户类使用了他们
的话则意味着灾难。
考虑这么一种情况:
你觉得Tlist在查找方面不能满足你的需要,所以你决定把Tlist改成TLinkList,那么你的客户类所有
用到Tlist方法的地方都要修改。而第二种则不存在这种情况,所有的修改都在TPJDData中。
套用一句术语就是:一个类的修改压力不能传递给其它类。
 
to mywyn;
交个朋友,一起研究模式:,我q 87579974
 
放点代码上来,一起研究:就是以上两种情况的例子, 各位发表意见.
1:
nit YWRZ;
interface
uses
SysUtils, Classes, DB, ADODB, UtilLib, JLDate,
State, dbEnv;
type
TYWRZ = class //业务日志
bh : integer;
//日志编号
czlx : string;
//操作类型
ywlx : string;
//业务类型
ywbh : string;
//业务编号
lszt : TMemoryStream;
//历史状态
slr : string;
//受理人
slsj : TJLDateTime;
//受理时间
bz : string;
//备注
zt : TState;
//状态
class function ReadFromDB(ABH: integer): TYWRZ;
procedure InsertDB;
procedure UpdateDB;
procedure DeleteDB;
destructor Destroy;
override;
end;

TYWRZList = class(TList)
function GetYWRZ(ABH : integer) : TYWRZ;
class function ReadFromDB: TYWRZList;
procedure InsertDB;
procedure UpdateDB;
procedure DeleteDB;
destructor Destroy;
override;
end;

//日志状态
const S_LSZT_WHF : TState = (bm: 1 shl 1;
mc: '未恢复');
const S_LSZT_YHF : TState = (bm: 1 shl 0;
mc: '已恢复');
//已恢复过
implementation
{ TYWRZ }
destructor TYWRZ.Destroy;
begin
if (lszt <> nil) then
lszt.Free;
if (slsj <> nil) then
slsj.Free;
inherited;
end;

class function TYWRZ.ReadFromDB(ABH: integer): TYWRZ;
var
ywrz : TYWRZ;
adocmd : TADOCommand;
adods : TADODataSet;
bField : TBlobField;
tbl_name : string;
begin
ywrz := nil;
tbl_name := 'TBL_YWRZ';
adocmd := dbpool.adocmd_pjgl;
adods := TADODataSet.Create(adocmd);
adocmd.Prepared := True;
with adocmddo
begin
commandtext := 'SELECT * FROM ' + tbl_name + ' WHERE ' +
'YWRZ_BH = :bh ' ;
with Parametersdo
begin
ParamByName('bh').Value := ABH;
end;
adods.RecordSet := Execute;
end;
if (adods.RecordCount = 1) then
begin
ywrz := TYWRZ.Create;
ywrz.bh := ABH;
ywrz.czlx := Trim(adods.FieldByName('YWRZ_CZLX').AsString);
ywrz.ywlx := Trim(adods.FieldByName('YWRZ_YWLX').AsString);
ywrz.ywbh := Trim(adods.FieldByName('YWRZ_YWBH').AsString);
ywrz.slr := Trim(adods.FieldByName('YWRZ_SLR').AsString);
ywrz.slsj := TJLDateTime.Create(Trim(adods.FieldByName('YWRZ_SLSJ').AsString));
ywrz.bz := Trim(adods.FieldByName('YWRZ_BZ').AsString);
ywrz.zt.bm:= adods.FieldByName('YWRZ_ZT_BM').AsInteger;
ywrz.zt.mc:= Trim(adods.FieldByName('YWRZ_ZT_MC').AsString);
ywrz.lszt := TMemoryStream.Create;
bField := TBlobField(adods.FieldByName('YWRZ_LSZT'));
bField.SaveToStream(ywrz.lszt);
end;
adods.free;
result := ywrz;
end;

procedure TYWRZ.InsertDB;
var
adocmd : TADOCommand;
tbl_name : string;
begin
tbl_name := 'TBL_YWRZ';
adocmd := dbpool.adocmd_pjgl;
adocmd.Prepared := True;
with adocmddo
begin
commandtext:='INSERT INTO ' + tbl_name +
'(YWRZ_CZLX, YWRZ_YWLX, YWRZ_YWBH, ' +
'YWRZ_LSZT, YWRZ_SLR, YWRZ_SLSJ, YWRZ_BZ, ' +
'YWRZ_ZT_BM, YWRZ_ZT_MC) ' +
' VALUES ' +
'(:czlx, :ywlx, :ywbh,' +
' :lszt, :slr, :slsj, :bz, ' +
' :zt_bm, :zt_mc)';
with Parametersdo
begin
ParamByName('czlx').Value := czlx;
ParamByName('ywlx').Value := ywlx;
ParamByName('ywbh').Value := ywbh;
ParamByName('slr').Value := slr;
ParamByName('slsj').Value := slsj.ToString;
ParamByName('zt_bm').Value := zt.bm;
ParamByName('zt_mc').Value := zt.mc;
if (not isEmpty(bz)) then
ParamByName('bz').Value := bz;
if (lszt <> nil) then
if (lszt.Size > 0) then
ParamByName('lszt').LoadFromStream(lszt, ftBlob);
end;
Execute;
end;
end;

procedure TYWRZ.UpdateDB;
var
adocmd : TADOCommand;
tbl_name : string;
begin
tbl_name := 'TBL_YWRZ';
adocmd := dbpool.adocmd_pjgl;
adocmd.Prepared := True;
with adocmddo
begin
commandtext:='UPDATE ' + tbl_name +
' SET YWRZ_CZLX = :czlx, YWRZ_YWLX = :ywlx ,YWRZ_YWBH = :ywbh,'+
' YWRZ_LSZT = :lszt, YWRZ_SLR = :slr, YWRZ_SLSJ = :slsj,' +
' YWRZ_BZ = :bz,' +
' YWRZ_ZT_BM= :zt_bm, YWRZ_ZT_MC = :zt_mc ' +
' WHERE KJND_BH = :kjnd_bh AND YWRZ_BH = :bh';
with Parametersdo
begin
ParamByName('bh').Value := bh;
ParamByName('czlx').Value := czlx;
ParamByName('ywlx').Value := ywlx;
ParamByName('ywbh').Value := ywbh;
ParamByName('slr').Value := slr;
ParamByName('slsj').Value := slsj.ToString;
ParamByName('bz').Value := bz;
ParamByName('zt_bm').Value := zt.bm;
ParamByName('zt_mc').Value := zt.mc;
ParamByName('lszt').LoadFromStream(lszt, ftBlob);
end;
Execute;
end;
end;

procedure TYWRZ.DeleteDB;
var
adocmd : TADOCommand;
tbl_name : string;
begin
tbl_name := 'TBL_YWRZ';
adocmd := dbpool.adocmd_pjgl;
adocmd.Prepared := True;
with adocmddo
begin
commandtext:='DELETE ' + tbl_name + ' WHERE ' +
'YWRZ_BH= :bh';
with Parametersdo
begin
ParamByName('bh').Value := bh;
end;
Execute;
end;
end;

{ TYWRZList }
destructor TYWRZList.Destroy;
var
ywrz : TYWRZ;
i : integer;
begin
for i := 0 to count -1do
begin
ywrz := items;
ywrz.Free;
end;
inherited;
end;

function TYWRZList.GetYWRZ(ABH: integer): TYWRZ;
var
i : integer;
ywrz : TYWRZ;
begin
for i := 0 to count -1do
begin
ywrz := items;
if (ywrz.bh = ABH) then
begin
result := ywrz;
exit;
end;
end;
result := nil;
end;

class function TYWRZList.ReadFromDB: TYWRZList;
var
ywrz : TYWRZ;
adocmd : TADOCommand;
adods : TADODataSet;
bField : TBlobField;
tbl_name : string;
ywrz_list : TYWRZList;
begin
ywrz_list:= nil;
tbl_name := 'TBL_YWRZ';
adocmd := dbpool.adocmd_pjgl;
adods := TADODataSet.Create(adocmd);
with adocmddo
begin
commandtext := 'SELECT * FROM ' + tbl_name + ' ORDER BY YWRZ_BH ASC ' ;
adods.RecordSet := Execute;
end;
if (adods.RecordCount > 0) then
begin
ywrz_list := TYWRZList.Create;
while (not adods.Eof)do
begin
ywrz := TYWRZ.Create;
ywrz.bh := adods.FieldByName('YWRZ_BH').AsInteger;
ywrz.czlx := Trim(adods.FieldByName('YWRZ_CZLX').AsString);
ywrz.ywlx := Trim(adods.FieldByName('YWRZ_YWLX').AsString);
ywrz.ywbh := Trim(adods.FieldByName('YWRZ_YWBH').AsString);
ywrz.slr := Trim(adods.FieldByName('YWRZ_SLR').AsString);
ywrz.slsj := TJLDateTime.Create(Trim(adods.FieldByName('YWRZ_SLSJ').AsString));
ywrz.bz := Trim(adods.FieldByName('YWRZ_BZ').AsString);
ywrz.zt.bm:= adods.FieldByName('YWRZ_ZT_BM').AsInteger;
ywrz.zt.mc:= Trim(adods.FieldByName('YWRZ_ZT_MC').AsString);
ywrz.lszt := TMemoryStream.Create;
bField := TBlobField(adods.FieldByName('YWRZ_LSZT'));
bField.SaveToStream(ywrz.lszt);
ywrz_list.Add(ywrz);
adods.Next;
end;
end;
adods.free;
result := ywrz_list;
end;

procedure TYWRZList.InsertDB;
var
i : integer;
ywrz : TYWRZ;
begin
for i := 0 to count -1do
begin
ywrz := items;
ywrz.InsertDB;
end;
end;

procedure TYWRZList.UpdateDB;
var
i : integer;
ywrz : TYWRZ;
begin
for i := 0 to count -1do
begin
ywrz := items;
ywrz.InsertDB;
end;
end;

procedure TYWRZList.DeleteDB;
var
i : integer;
ywrz : TYWRZ;
begin
for i := 0 to count -1do
begin
ywrz := items;
ywrz.DeleteDB;
end
end;

end.
2:
unit PJD_Data;
interface
uses
Windows, Messages, SysUtils, Classes, DB, ADODB,
PJD, JLDate, State, UtilLib, dbEnv, YWRZ;
type
TPJDNode = class
ywlx : string;
//业务类型
ywbh : string;
//业务编号
dm_bml :string;
//域编码链
dm_mc : string;
//域名称
yhpj_yh_mc : string;
//银行票据银行名称
yhpj_lx : string;
//银行票据类型
yhpj_qsbh : string;
//银行票据起始编号
yhpj_jsbh : string;
//银行票据结束编号
bz : string;
//备注
slr : string;
//最终受理人
slsj : TJLDateTime;
//最终受理时间
zt : TState;
//状态
procedure UpdateZT(AZT: TState);
destructor Destroy;
override;
end;

TPJDData = class
public
List : TList;
private
procedure FreeList;
public
function ReadPJD(ANode: TPJDNode): TPJD;
procedure AddNode(ANode: TPJDNode);
procedure DeleteNode(ANode: TPJDNode);
procedure Refresh;
destructor Destroy;
override;
end;

const
C_PJD_DATE_DAY: string = '本日';
C_PJD_DATE_WEEK: string = '本周';
C_PJD_DATE_MONTH_1:string = '一月';
C_PJD_DATE_MONTH_2:string = '二月';
C_PJD_DATE_MONTH_3:string = '三月';
C_PJD_DATE_MONTH_4:string = '四月';
C_PJD_DATE_MONTH_5:string = '五月';
C_PJD_DATE_MONTH_6:string = '六月';
C_PJD_DATE_MONTH_7:string = '七月';
C_PJD_DATE_MONTH_8:string = '八月';
C_PJD_DATE_MONTH_9:string = '九月';
C_PJD_DATE_MONTH_10:string = '十月';
C_PJD_DATE_MONTH_11:string = '十一月';
C_PJD_DATE_MONTH_12:string = '十二月';
C_PJD_DATE_YEAR :string = '全年';
var
pjddata : TPJDData;
implementation
{ TPJDData }
procedure TPJDData.AddNode(ANode: TPJDNode);
begin
if (list = nil) then
list := TList.Create;
list.Add(ANode);
end;

procedure TPJDData.DeleteNode(ANode: TPJDNode);
begin
list.Remove(ANode);
ANode.Free;
end;

destructor TPJDData.Destroy;
begin
FreeList;
inherited;
end;

procedure TPJDData.FreeList;
var
i :integer;
node :TPJDNode;
begin
if (List <> nil) then
begin
for i:=0 to List.Count-1do
begin
node := List.Items;
node.Free;
end;
List.Free;
List := nil;
end;
end;

function TPJDData.ReadPJD(ANode: TPJDNode): TPJD;
begin
result := TPJD.ReadFromDB(ANode.ywlx, ANode.ywbh);
end;

procedure TPJDData.Refresh;
var
adods : TAdoDataSet;
adocmd: TAdoCommand;
node : TPJDNode;
begin
FreeList;
adocmd := dbpool.adocmd_pjgl;
adocmd.Prepared := True;
adods := TADODataSet.Create(adocmd);
with adocmddo
begin
CommandText := 'SELECT * FROM TBL_PJDJ';
adods.Recordset := Execute;
end;
while (not adods.Eof)do
begin
node := TPJDNode.Create;
with nodedo
begin
ywlx := trim(adods.FieldByName('PJDJ_YWLX').AsString);
ywbh := trim(adods.FieldByName('PJDJ_YWBH').AsString);
dm_bml := trim(adods.FieldByName('PJDJ_DM_BML').AsString);
dm_mc := trim(adods.FieldByName('PJDJ_DM_MC').AsString);
yhpj_yh_mc := trim(adods.FieldByName('PJDJ_YHPJ_YH_MC').AsString);
yhpj_lx := trim(adods.FieldByName('PJDJ_YHPJ_LX').AsString);
yhpj_qsbh := trim(adods.FieldByName('PJDJ_YHPJ_QSBH').AsString);
yhpj_jsbh := trim(adods.FieldByName('PJDJ_YHPJ_JSBH').AsString);
slr := trim(adods.FieldByName('PJDJ_JBR').AsString);
slsj := TJLDateTime.Create(trim(adods.FieldByName('PJDJ_RQ').AsString));
bz := trim(adods.FieldByName('PJDJ_BZ').AsString);
zt.bm := adods.FieldByName('PJDJ_ZT_BM').AsInteger;
zt.mc := trim(adods.FieldByName('PJDJ_ZT_MC').AsString);
end;
AddNode(node);
adods.Next;
end;
adods.free;
end;

{ TPJDNode }
destructor TPJDNode.Destroy;
begin
if (slsj <> nil) then
slsj.Free;
inherited;
end;

procedure TPJDNode.UpdateZT(AZT: TState);
begin
zt := AZT;
end;

initialization
begin
if (pjddata = nil) then
pjddata := TPJDData.Create;
end;

finalization
begin
if (pjddata <> nil) then
begin
pjddata.Free;
pjddata := nil;
end;
end;

end.
 
顶到年底
 
以上的代码我看了一下,,
其实你的两个单元都用到了同一个模式,都对基础数据建立类,然后用TLIST去管理这个类的
实例,那么你就可以把这个管理类给抽象出来,给出虚拟方法,Insert ,Update,Delete,IniClass
任何一个基础数据类只要继承这个管理类便可以对他自己的数据进行管理,事实上你只要实现
Insert ,Update,Delete,IniClass这4个方法既可,这样的话就实现了面向接口编程,
其他的程序员用你的这个类的时候只要用这几个方法就行了,不用又用AddNode,InsertDB,
当然有可能这是两个不同的动作的,由于对你的业务不是很熟悉具体怎么整的话,
还得靠你自己。
 
你们这样会代码满天飞,真的会省力气吗?
实体类的数据直接用记录就可以了,干嘛用TLIST呢?
 
第一种用起来比较方便
第二种好扩展,但不好封装
 
class function ReadFromDB(ABH: integer): TYWRZ;
这句话什么意思,麻烦讲下阿
 

其实这两种方法都应该进一步改进一下:
1.为什么不把类中的属性用一个record来替代呢?TPJD就应该设计成record
2.如果采用第2种方法来建类的话,我想在TPJD中应该再加一个属性,整体改为:
TPJD= record
id :string;
name ;
string;
next:TPJD;
end;
3.我想你第1种方法建的类应该是以数组的形式来保存多条记录的吧,需第2种方法应该是
经链表指针的形式来保存的吧
 
这好像和设计模式没有关系吧,要把设计和实现分开来看
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部