Pau*_*aul 2 delphi oop optimization dry delphi-6
三类:TTntMemo,TTntEdit并TEdit有一个共同的祖先- TCustomEdit,但我不能使用Color和ShowHint性能TCustomEdit,因为它们protected并再为public只在TTntMemo,TTntEdit和TEdit.我不允许更改任何这些类,因为它们属于VCL或广泛使用的控件库.
以下代码是PITA,因为它必须重复三次 - 每种类型一次:
class procedure TCommon.ValidateEdit(edit: TCustomEdit; condition: Boolean;
failHint: WideString);
var m: TTntMemo;
te: TTntEdit;
e: TEdit;
begin
if edit is TTntMemo then begin
m := edit as TTntMemo;
if condition then begin
m.Color := clWindow;
m.Hint := '';
m.ShowHint := False;
end
else begin
m.Color := $AAAAFF;
m.Hint := failHint;
m.ShowHint := True;
end;
end
else
if edit is TTntEdit then begin
te := edit as TTntEdit;
if condition then begin
te.Color := clWindow;
te.Hint := '';
te.ShowHint := False;
end
else begin
te.Color := $AAAAFF;
te.Hint := failHint;
te.ShowHint := True;
end;
end;
if edit is TEdit then begin
e := edit as TEdit;
if condition then begin
e.Color := clWindow;
e.Hint := '';
e.ShowHint := False;
end
else begin
e.Color := $AAAAFF;
e.Hint := failHint;
e.ShowHint := True;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
不幸的是Delphi6没有反射.
您是否对如何优化此代码有一些想法?
使用砍死类的TCustomEdit
unit uCommon;
interface
uses
StdCtrls;
type
TCommon = class
class procedure ValidateEdit( AEdit : TCustomEdit; ACondition : Boolean; AFailHint : string );
end;
implementation
uses
Graphics;
type
// hacked TCustomEdit class to get access to protected properties
THackedCustomEdit = class( TCustomEdit )
published
property ShowHint;
property Color;
end;
{ TCommon }
class procedure TCommon.ValidateEdit( AEdit : TCustomEdit; ACondition : Boolean; AFailHint : string );
var
LEdit : THackedCustomEdit;
begin
LEdit := THackedCustomEdit( AEdit );
if ACondition
then
begin
LEdit.Color := clWindow;
LEdit.Hint := '';
end
else
begin
LEdit.Color := $AAAAFF;
LEdit.Hint := AFailHint;
end;
LEdit.ShowHint := not ACondition;
end;
end.
Run Code Online (Sandbox Code Playgroud)
或者你可以使用TypInfo单位和
uses
Graphics,
TypInfo;
class procedure TCommon.ValidateEdit( AEdit : TCustomEdit; ACondition : Boolean; AFailHint : string );
procedure SetPublishedPropValue( Instance : TObject; const PropName : string; const Value : Variant );
begin
if IsPublishedProp( Instance, PropName )
then
SetPropValue( Instance, PropName, Value );
end;
begin
if ACondition
then
begin
SetPublishedPropValue( AEdit, 'Color', clWindow );
AEdit.Hint := '';
end
else
begin
SetPublishedPropValue( AEdit, 'Color', $AAAAFF );
AEdit.Hint := AFailHint;
end;
SetPublishedPropValue( AEdit, 'ShowHint', not ACondition );
end;
Run Code Online (Sandbox Code Playgroud)
UPDATE
因为声明了所有属性,TControl所以也可以使用它作为基类而不是TCustomEdit
建议变得非常干燥
如果我要实现这样的验证器,我宁愿使用函数来获取ACondition值
unit uCommon;
interface
uses
Controls;
type
TCommon = class
class function ValidateControl( AControl : TControl; ACondition : Boolean; AFailHint : string ) : Boolean;
end;
implementation
uses
Graphics;
type
THackedControl = class( TControl )
published
property ShowHint;
property Color;
end;
{ TCommon }
class function TCommon.ValidateControl( AControl : TControl; ACondition : Boolean; AFailHint : string ) : Boolean;
var
LControl : THackedControl;
begin
// Return Condition as Result
Result := ACondition;
LControl := THackedControl( AControl );
if ACondition
then
begin
LControl.Color := clWindow;
LControl.Hint := '';
end
else
begin
LControl.Color := $AAAAFF;
LControl.Hint := AFailHint;
end;
LControl.ShowHint := not ACondition;
end;
end.
Run Code Online (Sandbox Code Playgroud)
在我的形式我会使用它(它将变得非常干)
function BoolAnd( AValues : array of Boolean ) : Boolean;
var
LIdx : Integer;
begin
Result := True;
for LIdx := Low( AValues ) to High( AValues ) do
begin
Result := Result and AValues[LIdx];
if not Result then
Break;
end;
end;
procedure TForm1.Validate;
begin
SaveButton.Enabled :=
BoolAnd( [
TCommon.ValidateControl( Edit1, Edit1.Text <> '', 'must not be empty' ),
TCommon.ValidateControl( Memo1, Memo1.Text <> '', 'must not be empty' ),
TCommon.ValidateControl( SpinEdit1, SpinEdit1.Value >= 10, 'must not be below 10' ),
TCommon.ValidateControl( ComboBox1, ComboBox1.ItemIndex >= 0, 'must not be empty' )
] );
end;
Run Code Online (Sandbox Code Playgroud)