抽象工厂的好处在于用户可根据不同的配置调用具体的工厂对象,从而获取不同的产品。
在设计时,实现产品创建和客户端的分离。客户端通过抽象接口获取所要的产品。使用反射的方法可使工厂方法变得更加灵活。
PS:相关源码参考大话设计模式C#版本。
{*------------------------------------------------------------------------------ 设计模式之抽象工厂 @author genispan @version @todo @comment Delphi版抽象工厂模式-------------------------------------------------------------------------------}unit Unit1;interfaceuses windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls;type TUser = class private { Private declarations } FID:Integer; Fname:string; public { Public declarations } property ID:Integer read FID write FID; property name:string read Fname write Fname; end; IUser = interface procedure Insert(AUser:TUser); function Get(ID:Integer):TUser; end; TCommonUser = class(TInterfacedPersistent,IUser) procedure Insert(AUser:TUser); virtual;abstract; function Get(ID:Integer):TUser; virtual;abstract; end; TsqlServerUser= class(TCommonUser) procedure Insert(AUser:TUser); overrIDe; function Get(ID:Integer):TUser; overrIDe; end; TOracleUser= class(TCommonUser) procedure Insert(AUser:TUser); overrIDe; function Get(ID:Integer):TUser; overrIDe; end; TDepartment = class private { Private declarations } FDeptID:Integer; FDeptname:string; public { Public declarations } property DeptID:Integer read FDeptID write FDeptID; property Deptname:string read FDeptname write FDeptname; end; IDepartment = interface procedure InsertDept(ADpt:TDepartment); function GetDept(DeptID:Integer):TDepartment; end; TCommonDept = class(TInterfacedPersistent,IDepartment) procedure InsertDept(ADept:TDepartment); virtual;abstract; function GetDept(DeptID:Integer):TDepartment;virtual;abstract; end; TsqlServerDepartment= class(TCommonDept) procedure InsertDept(ADept:TDepartment); overrIDe; function GetDept(DeptID:Integer):TDepartment; overrIDe; end; TOracleDepartment= class(TCommonDept) procedure InsertDept(ADept:TDepartment); overrIDe; function GetDept(DeptID:Integer):TDepartment; overrIDe; end; IFactory = interface function createuser:IUser; function CreateDpt:IDepartment; end; TFactory = class(TInterfacedobject,IFactory) private { Private declarations } public { Public declarations } function createuser:IUser; function CreateDpt:IDepartment; end; TForm1 = class(TForm) Memo1: TMemo; button1: Tbutton; procedure button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; fdbType:string; implementation{$R *.dfm}{TsqlServerUser}procedure TsqlServerUser.Insert(AUser:TUser);begin Form1.Memo1.lines.Add('sqlServer用户表增加一条记录!');end;function TsqlServerUser.Get(ID:Integer):TUser;begin Form1.Memo1.lines.Add(Format('sqlServer获取用户【%d】记录!',[ID])); Result:=nil;end;{TOracleUser}procedure TOracleUser.Insert(AUser:TUser);begin Form1.Memo1.lines.Add('Oracle用户表增加一条记录!');end;function TOracleUser.Get(ID:Integer):TUser;begin Form1.Memo1.lines.Add(Format('Oracle获取用户【%d】记录!',[ID])); Result:=nil;end;{TsqlServerDepartment}procedure TsqlServerDepartment.InsertDept(ADept:TDepartment);begin Form1.Memo1.lines.Add('sqlServere部门表增加一条记录!');end;function TsqlServerDepartment.GetDept(DeptID:Integer):TDepartment;begin Form1.Memo1.lines.Add(Format('sqlServer获取部门【%d】记录!',[DeptID])); Result:=nil;end;{TOracleDepartment}procedure TOracleDepartment.InsertDept(ADept:TDepartment);begin Form1.Memo1.lines.Add('Oracle部门表增加一条记录!');end;function TOracleDepartment.GetDept(DeptID:Integer):TDepartment;begin Form1.Memo1.lines.Add(Format('Oracle获取用户【%d】记录!',[DeptID])); Result:=nil;end;{TFactory}function TFactory.createuser:IUser;begin Result:= IUser(TCommonUser(FindClass('T'+fdbType+'User').Create));end;function TFactory.CreateDpt:IDepartment;begin Result:= IDepartment(TCommonDept(FindClass('T'+fdbType+'Department').Create));end;{TForm1}procedure TForm1.button1Click(Sender: TObject);var vUser:TUser; vDept:TDepartment; interfaceUser:IUser; interfaceDept:IDepartment; vFactory:TFactory;begin fdbType := 'Oracle'; vUser := TUser.Create; vDept := TDepartment.Create; vFactory:=TFactory.Create; interfaceUser := vFactory.createuser; interfaceUser.Insert(vUser); interfaceUser.Get(1); interfaceDept := vFactory.CreateDpt; interfaceDept.InsertDept(vDept); interfaceDept.GetDept(1);end;initialization RegisterClasses([TsqlServerUser,TOracleUser,TsqlServerDepartment,TOracleDepartment]);end.总结
以上是内存溢出为你收集整理的设计模式之抽象工厂全部内容,希望文章能够帮你解决设计模式之抽象工厂所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)