ERPSYSTEM开发教程05 框架接口管理

框架接口是采用类厂来管理的

一个接口要被框架管理的实现步骤

首先是接口定义,接口实现类,接口实例创建函数,指定类厂

unit BplOneIntf;
{$WEAKPACKAGEUNIT ON}

interface

type
  IBplOne=interface
    ['{E4C1C5FA-C598-4834-9111-26B52634068D}']
    function Add(A,B:Integer):integer;
    function Sub(A,B:Integer):Integer;
  end;

implementation

end.

这是接口的定义文件,如果一个接口是可以提供给其它模块调用的话,那么就放到一个公共目录,并加入Delphi搜索路径,方便调用

unit BplOneObj;

interface

uses sysUtils,Classes,SysFactory,BplOneIntf;//记得这里引用你的接口单元

Type
  TBplOne=Class(TInterfacedObject,IBplOne)//假设你的接口叫IXXX(以下同)
  private
  protected
  {IXXX}
    //这里加上你接口方法,然后按Ctrl+Shift+C,实现你的接口...
    function Add(A,B:Integer):integer;
    function Sub(A,B:Integer):Integer;
  Public
  End;

implementation

procedure Create_BplOne(out anInstance: IInterface);
begin
  anInstance:=TBplOne.Create;
end;
{ TBplOne }

function TBplOne.Add(A, B: Integer): integer;
begin
  result:=A+B;
end;

function TBplOne.Sub(A, B: Integer): Integer;
begin
  result:=A-B;
end;

var Factory:TObject;
initialization
  Factory:
=TIntfFactory.Create(IBplOne,@Create_BplOne);
finalization
  Factory.Free;
end.

这里分别是接口的实现类TBplOne,接口实例创建函数Create_BplOne(out anInstance: IInterface);以及接口工厂TIntfFactory的创建

框架提供了三种类厂TIntfFactory接口工厂,TSingletonFactory单实例工厂,TObjFactory实例工厂

其中TIntfFactory接口工厂,是在接口调用时,通过接口实例创建函数来创建实例对象

TSingletonFactory单实例工厂则是在第一次接口调用时通过接口实例创建函数来创建实例对象并保存该对象,再次调用时则返回先前创建的对象

TObjFactory实例工厂则是将一个已经创建好的对象与接口绑定,当调用接口时则返回该对象

单实例工厂与实例工厂都是只有一个实例对象,不同的是,单实例工厂是在第一次调用时创建对象,而实例工厂的实例则是预先创建好的

以接口工厂TIntfFactory为例,了解一下类厂是如何管理的

constructor TIntfFactory.Create(IID: TGUID; IntfCreatorFunc:TIntfCreatorFunc);
begin
  Flag:=0;
  self.FIntfCreatorFunc:=IntfCreatorFunc;
  Inherited Create(IID);
end;

constructor TBaseFactory.Create(const IID: TGUID);
begin
  if FactoryManager.Exists(IID) then
    Raise Exception.CreateFmt(Err_IntfExists,[GUIDToString(IID)]);

  FIntfGUID:=IID;
  FactoryManager.RegisterFactory(Self);
end;

procedure TSysFactoryManager.RegisterFactory(aIntfFactory: TFactory);
var i:Integer;
    IIDStr:String;
    IID:TGUID;
begin
  FSysFactoryList.Add(aIntfFactory);

  for i := FKeyList.Count - 1 downto 0 do
  begin
    IIDStr:=FKeyList[i];
    IID   :=StringToGUID(IIDStr);
    if aIntfFactory.Supports(IID) then
    begin
      FIndexList.Add(IIDStr,Pointer(aIntfFactory));
      FKeyList.Delete(i);
    end;
  end;
end;

TIntfFactory是TBaseFactory的子类,首先是保存了接口实例创建函数的地址,然后调用基类构造函数,

FactoryManager是一个全局函数,返回的是一个TSysFactoryManager类对象,该类是类厂的管理类,

先判断是否存在该接口的工厂对象,如果不存在则对接口进行注册,注册的步骤就是将接口工厂添加到工厂列表

这样就实现了接口类厂的注册。

那么统一的接口调用又是如何实现的呢?

统一接口调用 SysService as IXXX

function SysService:IInterface;
begin
  if not Assigned(FSysService) then
    FSysService:=TSysService.Create;
    
  Result:=FSysService;
end;

SysService是一个全局函数,返回的是一个接口IInterface,由TSysService构造的对象

Type
  TSysService=Class(TObject,IInterface)
  private
    FRefCount: Integer;
  protected
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
  public

  end;

function TSysService.QueryInterface(const IID: TGUID; out Obj): HResult;
var aFactory:TFactory;
begin
  Result:=E_NOINTERFACE;
  if self.GetInterface(IID,Obj) then
    Result:=S_OK
  else begin
    aFactory:=FactoryManager.FindFactory(IID);
    if Assigned(aFactory) then
    begin
      aFactory.CreateInstance(IID,Obj);
      Result:=S_OK;
    end;
  end;
end;

而TSysService重新实现了IInterface接口,当一个接口进行as操作的时候,就会调用QueryInterface进行接口查询

所以SysService as IXXX就会调用接口查询QueryInterface,

而接口查询函数首先是通过全局函数FactoryManager返回的TSysFactoryManager类对象,查找是否有该接口的类厂

如果存在该类厂则取出,并通过类厂创建实例返回。

以上就是框架的接口管理。

视频教程地址:

ERPSYSTEM开发教程01     http://pan.baidu.com/s/1kT7Rb3D

ERPSYSTEM开发教程02     http://pan.baidu.com/s/11xN5s

框架源代码下载地址            http://pan.baidu.com/s/1jGIc2Su

DEMO源代码下载地址         http://pan.baidu.com/s/1bnyEafH

联系QQ:1330009208 (验证信息请填ERPSYSTEM)

原文地址:https://www.cnblogs.com/erp-system/p/4224687.html