对于每个setter的类,我必须实现一些事件逻辑(OnChanging,OnChanged):
procedure TBlock.SetWeightIn(const Value: Double); var OldValue: Double; begin OldValue := FWeightIn; DoOnChanging(OldValue,Value); FWeightIn := Value; DoOnChanged(OldValue,Value); end; procedure TBlock.SetWeightOut(const Value: Double); var OldValue: Double; begin OldValue := FWeightOut; DoOnChanging(OldValue,Value); FWeightOut := Value; DoOnChanged(OldValue,Value); end;
你可以建议一种方法来实现这一点,而不是为每个设置者复制所有这些行?
解决方法
尝试这个:
procedure TBlock.SetField(var Field: Double; const Value: Double); var OldValue: Double; begin OldValue := Field; DoOnChanging(OldValue,Value); Field := Value; DoOnChanged(OldValue,Value); end; procedure TBlock.SetWeightIn(const Value: Double); begin SetField(FWeightIn,Value); end; procedure TBlock.SetWeightOut(const Value: Double); begin SetField(FWeightOut,Value); end;