使用RAD XE在Delphi中的另一个方法之前自动执行方法

时间:2014-02-17 14:57:00

标签: delphi delphi-xe

美好的一天,

我编写了一些类TFooTBar。他们有一些方法需要调用swithcing函数TFoo.switchTBar.switch

TBaseFooBar = class
  strict private
    FNumConfig: Word;
  protected
    procedure Switch;  
end;

这个超级类有一个私有字段FNumConfig,用于描述PCI卡的数量。 Switch方法使用某个库中的函数:

procedure TBaseFooBar.Switch;  
begin
  tmkselect(FNumConfig);
end;

我想问你有没有办法在执行任何SwitchDoSomethingWithFoo()之前调用超级函数DoSomethingWithBar()? 我的意思是这样的

TFoo = class(TBaseFooBar )
  { FNumConfig: Word; // this number declares the number of a PCI card}
  public 

  @Switch // before this method
  procedure DoSomethingWithFoo1();

// ...
  @Switch // and before this one
  procedure DoSomethingWithFooN();

// before this one there is no calling of Switch
  function NotSwitching();
end;

TBar

TBar = class(TBaseFooBar )
{  FNumConfig: Word; // this is another one number declares of another PCI card, they are not equal. }
public 
@Switch // before this method
  procedure DoSomethingWithBar1();
// ...
@Switch // and before this one
  procedure DoSomethingWithBarN();
end;

我知道一个简单的方法:

procedure TFoo.DoSomethingWithFoo1();  
begin
Switch();
// and another stuff.
end;

但我想把它全部写在一个地方 - 在一个类的声明中。

2 个答案:

答案 0 :(得分:1)

Delphi不支持内置的任何内容。您要求的内容本质上是面向方面的编程功能。第三方供应商提供了一些选项,have been discussed here before

答案 1 :(得分:0)

有一些面向对象的设计模式可能有助于解决这个问题:

  • 命令模式
  • 装饰图案
  • 策略模式

这意味着:操作可以实现为“Command”对象,在需要时由“Switcher”类装饰。