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

标签 delphi oop optimization dry delphi-6

这个问题在这里已经有了答案:





Accessing protected event of TWinControl

(1 个回答)


8年前关闭。




三类:TTntMemo , TTntEditTEdit有共同的祖先 - TCustomEdit ,但我不能使用 ColorShowHint TCustomEdit 的属性因为他们是protected并重新引入为 public仅在 TTntMemo , TTntEditTEdit .我不允许更改任何这些类,因为它们属于 VCL 或广泛使用的控件库。

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

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;

不幸的是,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.

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

关于delphi - 具有相同祖先的类的重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20654022/

相关文章:

c# - 从基本 Delphi 窗体派生 C# 窗体

delphi - Delphi 中的 TMutex 是可重入的吗?

windows - 如何检测 Delphi FMX Windows 窗体中的鼠标后退和前进按钮?

oop - Matlab子类化问题

delphi - 为什么 Delphi 编译器不针对重新定义的常量发出警告?

php - 新关键字与创建实例

java - Java 中的标记图表示

javascript - Google Optimize 不适用于激活事件(未设置页面位置)

Scala 的collectFirst 函数返回Option[U]

c - 比 fgets 更快地读取一行输入?