Delphi 灵活运用接口(interface), 隐藏核心代码, 设计低耦合程序.
2014-09-29 11:08
483 查看
2007年04月18日 星期三 08:34
原创作品, 如有转载请注明出处.
COPYRIGHT BY cnCharles, ALL RIGHTS RESERVED.
delphi群: 16497064, blog: http://hi.baidu.com/cnCharles
程序详细代码如下
program InterfaceTest;
uses
Forms,
Main in 'Main.pas' {frmMain},
Test in 'Test.pas' {frmTest},
PubIntfs in 'PubIntfs.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end.
//-----------------------------------------------------------------------------------------------
unit Main;
{
Date : 2007-04-18
Author : cnCharles
Description: 应用程序主界面单元
}
interface
uses
Forms, Dialogs, StdCtrls, Classes, Controls, PubIntfs;
type
TfrmMain = class(TForm, IMainFormTest)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
protected
procedure SayHello;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
uses Test;
{$R *.dfm}
procedure TfrmMain.Button1Click(Sender: TObject);
begin
with TfrmTest.Create(Self) do
Show;
end;
procedure TfrmMain.SayHello;
begin
ShowMessage('I''m a MainForm');
end;
end.
//--------------------------------------------------------------------------------------------------
unit Test;
{
Date : 2007-04-18
Author : cnCharles
Description: 接口测试
}
interface
uses
Forms, StdCtrls, Classes, Controls;
type
TfrmTest = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmTest: TfrmTest;
implementation
uses PubIntfs;
{$R *.dfm}
procedure TfrmTest.Button1Click(Sender: TObject);
begin
(Application.MainForm as IMainFormTest).SayHello;
end;
end.
//-------------------------------------------------------------------------------------------------
unit PubIntfs;
{
Date : 2007-04-18
Author : cnCharles
Description: 对外公开接口
}
interface
const
IID_MainFormTest = '{6E6F8E9C-E147-47DF-95F8-A5861DD8F393}';
type
IMainFormTest = interface
[IID_MainFormTest]
procedure SayHello;
end;
implementation
end.
不 知道你们有没有看明白, 在Test单元中并没有uses(引用) Main单元, 但是但是确可以调用它的 SayHello方法. 在这个Demo中还要明白一个要点, Application.CreateForm 第一次创建派生自TForm 的Form即为程序的主窗体. Application是一个全局变量, 定义在Forms单元中, 只要引用了Forms单元就可以引用Application, Screen对象也与Application一样.
原创作品, 如有转载请注明出处.
COPYRIGHT BY cnCharles, ALL RIGHTS RESERVED.
delphi群: 16497064, blog: http://hi.baidu.com/cnCharles
程序详细代码如下
program InterfaceTest;
uses
Forms,
Main in 'Main.pas' {frmMain},
Test in 'Test.pas' {frmTest},
PubIntfs in 'PubIntfs.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end.
//-----------------------------------------------------------------------------------------------
unit Main;
{
Date : 2007-04-18
Author : cnCharles
Description: 应用程序主界面单元
}
interface
uses
Forms, Dialogs, StdCtrls, Classes, Controls, PubIntfs;
type
TfrmMain = class(TForm, IMainFormTest)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
protected
procedure SayHello;
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
uses Test;
{$R *.dfm}
procedure TfrmMain.Button1Click(Sender: TObject);
begin
with TfrmTest.Create(Self) do
Show;
end;
procedure TfrmMain.SayHello;
begin
ShowMessage('I''m a MainForm');
end;
end.
//--------------------------------------------------------------------------------------------------
unit Test;
{
Date : 2007-04-18
Author : cnCharles
Description: 接口测试
}
interface
uses
Forms, StdCtrls, Classes, Controls;
type
TfrmTest = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmTest: TfrmTest;
implementation
uses PubIntfs;
{$R *.dfm}
procedure TfrmTest.Button1Click(Sender: TObject);
begin
(Application.MainForm as IMainFormTest).SayHello;
end;
end.
//-------------------------------------------------------------------------------------------------
unit PubIntfs;
{
Date : 2007-04-18
Author : cnCharles
Description: 对外公开接口
}
interface
const
IID_MainFormTest = '{6E6F8E9C-E147-47DF-95F8-A5861DD8F393}';
type
IMainFormTest = interface
[IID_MainFormTest]
procedure SayHello;
end;
implementation
end.
不 知道你们有没有看明白, 在Test单元中并没有uses(引用) Main单元, 但是但是确可以调用它的 SayHello方法. 在这个Demo中还要明白一个要点, Application.CreateForm 第一次创建派生自TForm 的Form即为程序的主窗体. Application是一个全局变量, 定义在Forms单元中, 只要引用了Forms单元就可以引用Application, Screen对象也与Application一样.
相关文章推荐
- Delphi 灵活运用接口(interface), 隐藏核心代码, 设计低耦合程序.
- objc@interface的设计:隐藏继承关系中的私有接口
- .NET核心代码保护策略-隐藏核心程序集
- 设计模式之Programming to an Interface, not anImplementation 程序指向接口,而不是实现
- 设计模式之Programming to an Interface, not anImplementation 程序指向接口,而不是实现
- JAVA进阶 面向对象程序设计——第5周 设计原则(耦合和聚合,代码结构设计,程序维护拓展发展)
- 架构,改善程序复用性的设计~目录(附核心原代码)
- 在Delphi中隐藏程序进程的方法[2]--纯DELPHI代码方式
- Delphi 设计模式:《HeadFirst设计模式》Delphi代码---模式小结之一个叫声接口和几只鸭子
- 设计模式精解-第三章-一个急需灵活代码的问题
- 汉王扫描仪调用接口,delphi代码
- 面向接口设计(Interface Oriented Design)
- 看代码设计中的“隐形接口”
- .Net集合类型大全,只要灵活运用可以节省很多开发时间和提高程序运行效率
- C#类设计时,何时用类继承(Base Class)何时用接口(Interface)
- 接口介绍(在.NET下编写中文代码程序)
- WAS应用系列:WAS6 自定义用户注册接口代码 + 企业应用安全角色配置 + WAS安全配置 实现统一灵活的集成授权
- 熊猫烧香核心代码(delphi)
- Collection -> .Net集合类型大全,只要灵活运用可以节省很多开发时间和提高程序运行效率
- 用C#设计一个驱动备份的程序代码