我经常会写一些像这样使用的类:
- 创建一个类的对象
- 在该对象上调用某些“Run”或“Execute”方法
- 销毁该对象
这会给调用代码增加一些开销,例如:
var
Foo: TFoo;
begin
Foo := TFoo.Create(...);
try
Foo.Run(...);
finally
Foo.Free;
end;
end;
这段话可以简化为以下内容:
begin
TFoo.Run(...);
end;
在这种情况下,包含
TFoo
类的单元将如下所示:type
TFoo = class
private
FBar: TBar;
procedure InternalRun;
public
class procedure Run(ABar: TBar); static;
end;
class procedure TFoo.Run(ABar: TBar);
var
Foo: TFoo;
begin
Foo := TFoo.Create;
try
Foo.FBar := ABar;
Foo.InternalRun;
finally
Foo.Free;
end;
end;
这种设计模式的名字是什么?
调用代码的开销被转移到了TFoo
类中。