How / if to refactor a Delphi program using only forms and data modules

前端 未结 6 1607
Happy的楠姐
Happy的楠姐 2020-12-23 22:09

After years of coding Delphi programs as untestable code in forms and datamodules, including global variables, and the only classes are the forms themselves, containing all

6条回答
  •  醉梦人生
    2020-12-23 22:50

    If I encounter a form (or other class) with too much responsibility, I usualy follow the pattern below:

    1. Define a new class for the logic.
    2. Create a member variable of the new class in the form.
    3. Create the class in the onCreate and free it in the onDestroy of the form.
    4. Move a single piece of logic (for example a variable) to the new class.
    5. Move or create all methods to the new class.
    6. Compile and test.
    7. Continue until all logic is put in the new class.
    8. Try to decouple the logic class from the form class. (You can even work with interfaces if you like).

    There are situations where a single class is not enough, so it is no problem to create more classes. And these classes can have other classes to.

    With these steps, you can tackle most of these problems.

提交回复
热议问题