主程序启动时,将加载所有的插件,在运行过程中调用某个接口时,将会向一个PluginLoader请求该接口,该PluginLoader会返回一个插件变量给调用者,而它是使用在bpl中的类来完成该调用.
over.
下面给出一个bplLoader类的代码例子,它可以被你的主程序调用,就是插件管理类
{*******************************************************}
{
codemyth.Group
copyright 2004-2005
codemyth(at)gmail(dot)com
Create at 2005-7-20 11:22:26
插件容器类,用于载入插件
Change history:
}
{*******************************************************}
unit uPluginLoader;
interface
uses codemyth.utils, codemyth.util.objectlist, uIPlugin, Xmlplugin, Classes,
SysUtils;
type
TPluginLoader = class( TObject )
private
FPluginList: TObjectList; //存储插件调用接口
function GetPlugin( const id: string ): IPlugin;
function GetCount: integer;
function GetPluginByIndex( const index: integer ): IPlugin;
protected
procedure UnloadPlugin( const id: string ); overload; //卸载指定的插件
procedure UnloadPlugin( const index: Integer ); overload; //卸载指定的插件
procedure LoadPlugin( const XmlFile: string ); //载入位于某目录下的插件
procedure UnloadPlugins; //卸载所有裁入的插件接口
public
constructor Create;
destructor Destroy; override;
public
procedure LoadPlugins( Directory: string ); //载入插件
property Plugin [const id: string]: IPlugin read GetPlugin;
property PluginByIndex [const index: integer]: IPlugin read
GetPluginByIndex;
property Count: integer read GetCount;
end;
implementation
{ TPluginLoader }
constructor TPluginLoader.Create;
begin
FPluginList := TObjectList.Create;
end;
destructor TPluginLoader.Destroy;
begin
UnloadPlugins;
FPluginList.Free;
inherited;
end;
function TPluginLoader.GetCount: integer;
begin
result := FPluginList.Count;
end;
function TPluginLoader.GetPlugin( const id: string ): IPlugin;
var
index : Integer;
begin
index := FPlugi