Search code examples
delphioopoptimizationdrydelphi-6

Repeated code for classes with same ancestor


Three classes: TTntMemo, TTntEdit and TEdit have a common ancestor - TCustomEdit, but I can't use Color and ShowHint properties of TCustomEdit because they are protected and are reintroduced as public only in TTntMemo, TTntEdit and TEdit. I am not allowed to change any of these classes because they belong either to VCL or to widely used controls libraries.

Following code is a PITA because it has to repeat itself three times - one time for each type:

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;

Unfortunately Delphi6 doesn't have reflection.

Do you have some ideas how this code could be optimized?


Solution

  • Use a hacked class of 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.
    

    or you can use TypInfo unit and

    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;
    

    UPDATE

    Because all of the properties are declared in TControl you can also use this as your base class instead of TCustomEdit

    Suggestion to get very DRY

    If I would implement such a validator, I would prefer to use a function to get back the 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.
    

    In my form I would use this (and it will become very 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;