如何/仅仅使用表单和数据模块重构Delphi程序 [英] How / if to refactor a Delphi program using only forms and data modules

查看:139
本文介绍了如何/仅仅使用表单和数据模块重构Delphi程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多年的编译Delphi程序,在表单和数据模块中包含全局变量,而唯一的类是表单本身,包含表单UI本身所需的所有代码。

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 the code I need for the form UI itself.

如何将代码转换为一组实际工作的类?我需要停止使用datasources / datasets并在课堂上做一切吗?我需要一个ORM吗?

How would I convert the code to a set of classes that do the actual work? would I need to stop using the datasources/datasets and do everything in classes? do I need an ORM?

通常需要在表单中重用代码,所以转换逻辑是有意义的到课程?

There's usually zero need for reuse of the code in the forms, so does it make sense to convert the logic to classes?

推荐答案

如果我遇到一个有太多责任的表格(或其他类),我通常遵循以下模式:

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


  1. 为逻辑定义一个新类。

  2. 创建新类的成员变量

  3. 在onCreate中创建类,并在表单的onDestroy中释放该类。

  4. 移动单个逻辑(例如

  5. 将新方法移动或创建到新类。

  6. 编译和测试

  7. 继续,直到所有逻辑都放在新类中。

  8. 尝试将逻辑类与表单类分离。 (如果你喜欢,你甚至可以使用界面)。

  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.

这篇关于如何/仅仅使用表单和数据模块重构Delphi程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆