涉及程序整体框架时,考虑使用的一个方法;
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
从这个程序中应该能看到,它们之间的耦合性很高;所以代码不好;
所以要考虑用我说的这个方法解决个人观点,仅供参考

解决方案 »

  1.   

    怎么没有理我,好东西大家怎么不认呢?是看不懂吗?还是觉得没用??????
      

  2.   

    建议大家不要把问题集中在一点上,要注重程序的整体框架,只有框架的合理才能谈得上程序的合理,强壮;如果只注重编成,我们如何发展,我们永远是井底之蛙;多多注重程序的整体设计吧。可能我是杞人忧天
      

  3.   

    嗨,以后我还是不要设这样的讨论了,每人来,可怜那!!!!!!!!
    是不是我的思路太古老了,你们都会了
      

  4.   

    我非常同意你的看法,很好的结构,我也正在寻找这样的东西,