TFrame inheritance refactoring

后端 未结 2 451
花落未央
花落未央 2021-02-10 23:42

Yet another TFrame IDE-registered-component question from me. Thanks for all the help, fellow programmers. : )

Playing around with Darrian\'s TFrame inheritance suggest

2条回答
  •  无人共我
    2021-02-11 00:17

    I'm now developing a few "sister" components which will share a great deal of the existing component's non-visual functionality and properties. It makes sense, then, to move a lot of that to a parent/superclass which both the new and the old components can then inherit from.

    What is the best way to "refactor" TFrame inheritance in this way?

    The crux of your text above perhaps is "component's non-visual functionality". So, in this case, IMHO it's best to separate the visual and non-visual layers.

    So, perhaps it's better to use a decorator:

    TMySharedEngine = class(Whatever)
    property LinkedFrame: TFrame;
    property P1;
    property P2;
    ...
    procedure Proc1;
    procedure Proc2;
    ... //etc.
    end;
    

    and in your 'sister' frames to use instances of it:

    var
    TMyFrame1 = class(TFrame)
    ...
    FDecorator: TMySharedEngine;
      ...
      public
      property MySharedPart: TMySharedEngine read FDecorator;
      constructor Create(AOwner: TComponent); override;
      ...
    end;
    
    constructor TMyFrame1.(AOwner: TComponent); override;
    begin
      inherited;
      FDecorator:=TMySharedEngine.Create; //ok, ok do not forget to Free it .Destroy
      FDecorator.LinkedFrame:=Self;
      ...
    end;
    

    OTOH, if you want to use your approach you can use Visual Form Inheritance (as Darian suggested) or (more flexible) you can do it by hand: Create, using the IDE the following frames: TBaseFrame, TChildFrame1, TChildFrame2 ... etc. Now go on TChildFrame1's unit and change by hand it's class definition from TChildFrame1 = class(TFrame) to TChildFrame1 = class(TBaseFrame). Compile. It should work. It's recommended though that when you'll do this trick TBaseFrame to be empty in order to avoid possible small quirks (feature collisions etc.)

    HTH.

提交回复
热议问题