今天用了下class helper,觉得颇有意思.或许好好发展一下可以引起程序设计的小小变革.帮助上的例子如下:
type
TMyClass = class
procedure MyProc;
function MyFunc: Integer;
end; ... procedure TMyClass.MyProc;
var X: Integer;
begin
X := MyFunc;
end; function TMyClass.MyFunc: Integer;
begin
...
end;...type
TMyClassHelper = class helper for TMyClass
procedure HelloWorld;
function MyFunc: Integer;
end; ... procedure TMyClassHelper.HelloWorld;
begin
writeln(Self.ClassName); // Self refers to TMyClass type, not TMyClassHelper
end; function TMyClassHelper.MyFunc: Integer;
begin
...
end;
...
var
X: TMyClass;
begin
X := TMyClass.Create;
X.MyProc; // Calls TMyClass.MyProc
X.HelloWorld; // Calls TMyClassHelper.HelloWorld
X.MyFunc; // Calls TMyClassHelper.MyFunc这样可以不用派生就扩展原来的组件的功能。
那么对于一个已有的系统,扩展功能将更加方便。而不需要派生一个类,然后把原来代码中的引用该类的名字改掉。
也许就设计模式来讲,这种复用模式不好。但从工业应用,响应快速开发的角度来看,应该是相当不错的。但是class helper不支持虚函数,不支持成员变量,支持静态成员变量.支持普通的成员方法如果
能够做到支持成员变量.或者能够支持虚函数,并让class helper中的函数可以与其"派生"的类中
的虚方法进行交互.(关键是"派生"类的方法可以驱动class helper中的方法). 这样的话或者会开
创一种新的软件设计模式,呵呵,谁知道呢.
type
TMyClass = class
procedure MyProc;
function MyFunc: Integer;
end; ... procedure TMyClass.MyProc;
var X: Integer;
begin
X := MyFunc;
end; function TMyClass.MyFunc: Integer;
begin
...
end;...type
TMyClassHelper = class helper for TMyClass
procedure HelloWorld;
function MyFunc: Integer;
end; ... procedure TMyClassHelper.HelloWorld;
begin
writeln(Self.ClassName); // Self refers to TMyClass type, not TMyClassHelper
end; function TMyClassHelper.MyFunc: Integer;
begin
...
end;
...
var
X: TMyClass;
begin
X := TMyClass.Create;
X.MyProc; // Calls TMyClass.MyProc
X.HelloWorld; // Calls TMyClassHelper.HelloWorld
X.MyFunc; // Calls TMyClassHelper.MyFunc这样可以不用派生就扩展原来的组件的功能。
那么对于一个已有的系统,扩展功能将更加方便。而不需要派生一个类,然后把原来代码中的引用该类的名字改掉。
也许就设计模式来讲,这种复用模式不好。但从工业应用,响应快速开发的角度来看,应该是相当不错的。但是class helper不支持虚函数,不支持成员变量,支持静态成员变量.支持普通的成员方法如果
能够做到支持成员变量.或者能够支持虚函数,并让class helper中的函数可以与其"派生"的类中
的虚方法进行交互.(关键是"派生"类的方法可以驱动class helper中的方法). 这样的话或者会开
创一种新的软件设计模式,呵呵,谁知道呢.
解决方案 »
- 简体系统写的软件,在繁体系统下导出excel乱码
- 我想做一个ISDN的来电显示程序,给点思路!!!!!
- 怎样判断一个数是不是整数
- delphi高手请进!寻求帮助[提供项目资金]
- memo.lines.delete问题!
- 大家用过fastreport2.5的分栏打印有问题?
- XLSheet或者XLGrid谁用过呀.怎样能在程序里修改单元格合并?
- 高分,求高人指点迷津!!! 对于delphi调用vc Dll函数,参数传地址的方式如何完成!!!
- win 2000下用程序如何把一文件拷到另一台机器的某一目录下?谢谢!
- 使用过delphi+Acess的高手请进,只要能解决问题全部分数送上!
- 关于SQL2000中的视图
- 怎样纪录三层结构中连接的客户端个数?
I started off using Class Helpers to add library code to VCL objects, for library code. I now find myself using them as a design decision, putting minimalist interfaces on my objects, and adding all the helper and nicety functions as helper objects. As Marco Cantu points out, it looks like the next version of C# will support a similar feature. Personally I feel its another great feature of Delphi giving us a competitive advantage when developing with Object Pascal.
类设计最简单的接口,使用class helper来丰富接口以便与使用。这样,由类保持设计上的完善性,由class helper保证变化上的支持。我的感觉是,可以由类保持相对稳定的设计,由class helper保证具体的,多变的应用需求变化。