使用DLL封装业务逻辑

{
 CopyRight (c) 2006.10.27 咏南工作室
 ToDo: 使用DLL封装企业业务逻辑
 作者: 陈新光
   QQ: 254072148
 邮箱: hnxxcxg@yahoo.com.cn
}

{
 界面部分和业务部分物理分离。将业务部分和界面部分分别编译成dll和exe可执行文件。

 定义一个接口部分以实现界面部分和业务部分之间通讯。接口部分可使用接口或抽象类来实现。一定要保持接  口的稳定,轻易不要改变接口单元中的内容。还可通过预留接口来满足未来功能增长的需求。

 经常变动的通常是业务部分,通过修改DLL封装对象的实现方法来升级DLL,无需重新修改和编译界面部分的    Exe文件。
}

//-----------------------------------------------------------------------------------------------
{
 业务逻辑单元
}

unit cl;

interface

uses
  sysutils, dialogs, classes, forms,
  it;  //引用接口单元

type
  tTest = class(Tinterfacedobject, Ii)
  public
    procedure say;
    procedure say2;
    procedure say3; //预留接口
  end;

implementation

{ tTest }

procedure tTest.say;
begin
  showmessage('hello');
end;

procedure tTest.say2;
begin
  application.MessageBox('hello', 'dll', 64);
end;

procedure tTest.say3;
begin
  //showmessage('预留接口');
  {
   预留接口是为了应付未来客户对新功能的增加需求.
   例如: 将第一行的"//"注释符去掉. 然后只要重新编译"dll.dll",
   无须重新编译客户端程序. 客户端即增加了此项新功能.
  }
end;

end.
//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------
{
 接口单元
 也可使用抽象类作接口单元,但接口是最好的。
}

unit It;

interface

type
  Ii = interface
    ['{8C8C7999-0B73-4A85-B89E-3D8A8336FF06}'] //GUID可注释掉(可要可不要),非com项目建议去掉
    procedure say;
    procedure say2;
    procedure say3;
  end;

implementation

end.
//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------
{
 动态链接库工程文件
 请注意这里使用DLL封装对象的技巧, 对象在dll建立
}

library dll;

uses
  SysUtils,
  dialogs,
  Classes,
  cl in 'cl.pas',  //引用业务单元
  It in 'It.pas';  //引用接口单元

{$R *.res}

function hello: Ii;
begin
  result := tTest.Create;
end;

exports
  hello;

begin
end.
//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------
{
 客户端工程文件
}
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  It in 'It.pas';  //引用接口单元

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------
{
 客户端界面单元
 真正的瘦客户端,不包含任何企业逻辑代码
}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,
  it; //引用接口单元

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    m: Ii;  //定义变量
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  function hello: ii; external 'dll.dll';  //装载dll.dll

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  m := hello;
  m.say;
  m := nil;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  m := hello;
  m.say2;
  m := nil;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  m := hello;
  m.say3;
  m := nil;
end;

end.
来源:http://www.delphibbs.com/keylife/iblog_show.asp?xid=25173

posted @ 2010-08-25 23:23  覆雨翻云  阅读(252)  评论(0编辑  收藏  举报