Should Class Helpers be used in developing new code?

前端 未结 10 1649
眼角桃花
眼角桃花 2021-02-14 10:51

Delphi 8 introduced Class Helpers for the purposes of mapping the VCL/RTL to the .NET object hierarchy. They allow injecting methods into an existing class without overriding t

相关标签:
10条回答
  • 2021-02-14 11:09

    I find myself using them more and more as a design construct.

    Situations in which I use them :

    • In a client/server setup, I extend shared base-classes with class helpers to provide server- or client-only functionality.
    • To complement VCL/RTL classes (and other third party code) with handy tooling functions.
    • To work around differences when classes don't share the same inheritance tree (using helpers makes it possible to have have generic Count and Items properties, for example).

    In fact, I wish Delphi would accept multiple helpers for the same base class - I've even filed a request for this if I'm remembering correctly.

    0 讨论(0)
  • 2021-02-14 11:14

    I use them a lot. I use Remote Objects and the objects there are created by the RO engine so you cannot add to them without descending from them and then other bits of messing around. Class Helpers mean I can treat them like any other object. and while a class can only have one helper, you can descend helper classes so you get the inherited behaviour.

    0 讨论(0)
  • 2021-02-14 11:17

    Sorry, can't help but be Captain Obvious for a moment: If the internal Delphi people themselves state "they should not be viewed as a design tool to be used when developing new code" then by definition they shouldn't be used. They are there for extending the VCL for their own purposes only. Who else is going to give you a better reason than the people that wrote it?

    0 讨论(0)
  • 2021-02-14 11:18

    Before embracing class helpers as a new tool for fancy code, I think you have to understand the limitations is includes. There is only possible to provide one class helper for one class. So what will happen if you provide class helpers for your classes, and your classes derives from a common class that some other have provided a class helper for?

    CodeGear introduces class helpers as 'a hack' to prevent breaking things, not as a cool design feature. When you design code, design it without class helpers. I know you can. When dealing with existing code that you can control, use refactoring. When there is no other way, reach for class helpers.

    Thats my opinion any way...

    0 讨论(0)
  • 2021-02-14 11:18

    I found this article very interesting. It deals with C++ but the main ideas are language independent. The main gist is that global routines are sometimes preferrable to methods even in an OOP environment. From this view point, there's less need for class helpers.

    0 讨论(0)
  • 2021-02-14 11:19

    Maybe a good aproach you can use is (as I use it):

    1. Always give preference to inheritance over class helpers, use them only when inheritance is not an option.
    2. Give preference to Class helpers over bare global methods.
    3. If you're going to need the extendend functionality in more than a Unit, try something else (like class wrappers).

    .Net Extensions methods are way too similar and where created and supported for the exactly same reason: Make an Extention of the base classes (rather than an upgrade wich in Delphi.Net was not an option in order to try to make Delphi native code kind of "compatible" with .Net code - IMHO this was too ambitious)

    Anyway, Delphi Class helpers are still quite a tool in some situations.

    0 讨论(0)
提交回复
热议问题