涉及程序整体框架时,考虑使用的一个方法;
Dll,interface,MainApplication;1、先定义一个接口:
*****************************
unit myinterface;interfacetype
ImyTest = interface
['{8068AF74-187F-4847-9A5D-915CB4AF8FC7}']
function F1:integer;
end;
myTestClass = Class(TinterfacedObject,ImyTest)
function F1:integer;virtual;abstract;
end;implementation{ myTestClass }{
function myTestClass.F1: integer;
begin
result := 999;
end;
}end.
*****************************
2、在动态库中对其接口进行实现;
****************************
library Project2;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
myinterface,
Classes;type
myTest = Class(myTestClass)
function F1:integer;override;
end; {$R *.res}{ myTest }function getMyClass:myTest;
var
my:myTest;
begin
my := myTest.Create();
result := my;
end;
exports
getMyClass;
{ myTest }function myTest.F1: integer;
begin
result := 1000;
end;begin
end.
当然这就是你封在动态库里的窗体,只是将父类改称冲接口继承的类即可;
********************************
3、主程序引用接口,调用动态库中的窗体,调用其中的功能函数;
********************************
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,myinterface;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function getMyClass:myTestClass;external 'dll\Project2.dll';
var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
my:ImyTest;
begin
my := getMyClass().Create();
showmessage(inttostr(my.F1));
end;end.
***********************************
这样实现了功能模块与主程序模块的或者其他模块的分离,降低了耦合性;
***********************************
问题的引出
***********************************
通常有些人,想把窗体封装到动态库中,这就涉及到一个问题;
1、如果动态库中的窗体有自己的功能函数,主程序如果想调用,是看不见的,
只有引用动态库中的窗体,这样主程序就和动态库中的窗体的耦合性增加了;
另一方面,动态库的编写者的代码被公开了;容易被修改,导致整体项目的混乱;
2、所以应用接口的方法,实现了两者的分离;程序的整体结构以维护,小组同时工作
的可能性增加;///////////////////////////
请看程序不合理的代码,同样是我写的;见笑,
程序有两个包,包1调用包2,主程序调用包1,包2
从这个程序中应该能看到,它们之间的耦合性很高;所以代码不好;
所以要考虑用我说的这个方法解决个人观点,仅供参考
Dll,interface,MainApplication;1、先定义一个接口:
*****************************
unit myinterface;interfacetype
ImyTest = interface
['{8068AF74-187F-4847-9A5D-915CB4AF8FC7}']
function F1:integer;
end;
myTestClass = Class(TinterfacedObject,ImyTest)
function F1:integer;virtual;abstract;
end;implementation{ myTestClass }{
function myTestClass.F1: integer;
begin
result := 999;
end;
}end.
*****************************
2、在动态库中对其接口进行实现;
****************************
library Project2;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
myinterface,
Classes;type
myTest = Class(myTestClass)
function F1:integer;override;
end; {$R *.res}{ myTest }function getMyClass:myTest;
var
my:myTest;
begin
my := myTest.Create();
result := my;
end;
exports
getMyClass;
{ myTest }function myTest.F1: integer;
begin
result := 1000;
end;begin
end.
当然这就是你封在动态库里的窗体,只是将父类改称冲接口继承的类即可;
********************************
3、主程序引用接口,调用动态库中的窗体,调用其中的功能函数;
********************************
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,myinterface;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function getMyClass:myTestClass;external 'dll\Project2.dll';
var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
my:ImyTest;
begin
my := getMyClass().Create();
showmessage(inttostr(my.F1));
end;end.
***********************************
这样实现了功能模块与主程序模块的或者其他模块的分离,降低了耦合性;
***********************************
问题的引出
***********************************
通常有些人,想把窗体封装到动态库中,这就涉及到一个问题;
1、如果动态库中的窗体有自己的功能函数,主程序如果想调用,是看不见的,
只有引用动态库中的窗体,这样主程序就和动态库中的窗体的耦合性增加了;
另一方面,动态库的编写者的代码被公开了;容易被修改,导致整体项目的混乱;
2、所以应用接口的方法,实现了两者的分离;程序的整体结构以维护,小组同时工作
的可能性增加;///////////////////////////
请看程序不合理的代码,同样是我写的;见笑,
程序有两个包,包1调用包2,主程序调用包1,包2
从这个程序中应该能看到,它们之间的耦合性很高;所以代码不好;
所以要考虑用我说的这个方法解决个人观点,仅供参考
解决方案 »
- 一直提示“Invalid pointer operation”
- 怎样在DBGrid中点击某一条记录,使之一行全变蓝?
- 这样的组件布局怎么才能实现
- 开发组件的高手快进来,我们来互相交流
- Bpl内各窗体如何内部调用?
- 关于listbox使用了lbVirtual风格后的问题(急)
- 关于datatimepicker的题目
- 请问InsideDelphi。能否帮我解决实现从右往左输入的问题。在demo控件里。
- spcom的commname为com10时,只要com后面的数字大于9,为两位数,就出错。我用的串口服务器是moxa,用超级终端可以看到数据的传输,但用spcom一startcom就出错。请各位给予帮助。谢谢。
- 关于动态建表的问题?
- ADOTable的行数汇总出错?
- 请问怎么写一个搜索某一类文件的函数啊?
是不是我的思路太古老了,你们都会了