具有相同祖先的类的重复代码

时间:2013-12-18 09:22:28

标签: delphi oop optimization dry delphi-6

三个类:TTntMemoTTntEditTEdit有一个共同的祖先 - TCustomEdit,但我不能使用Color和{{1} } ShowHint的属性,因为它们是TCustomEdit,并且仅在protectedpublicTTntMemo中重新引入TTntEdit。我不允许更改任何这些类,因为它们属于VCL或广泛使用的控件库。

以下代码是PITA,因为它必须重复三次 - 每种类型一次:

TEdit

不幸的是Delphi6没有反射。

您是否了解如何优化此代码?

1 个答案:

答案 0 :(得分:7)

使用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.

或者您可以使用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;

<强>更新

因为所有属性都在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.

在我的表格中,我会使用它(它将变得非常 DRY

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;