组织代码,分段文件的逻辑布局

发布于 2024-08-26 20:00:35 字数 1435 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-09-02 20:00:35

我将尝试在这里表达我以前项目的经验,也许它们对你有帮助。

如果您尝试对项目进行分段,请尝试找到本身可能有用的组件。如果您编写数据库层,请考虑如何使数据库层独立于应用程序的其余部分(实用程序类和配置除外)。如果您编写一个访问数据库层的 RichClient,请尝试将您需要的内容准确地放入其中,这些内容在从 Web 层访问它时也需要。现在您拥有了一个在命令行客户端中甚至可能有用的组件。

或者,如果您想从较低的级别看到这一点,请将您的应用程序拆分为小单元,并且不要让这些单元具有循环依赖关系!!如果两个组件具有循环依赖关系,则它们无法拆分,并且实际上应该是单个组件。我知道,我有时会违反这条规则,因为你不能总是坚持下去,但这是一个很好的规则,可以帮助你了解应用程序的构建块。

应用程序的另一个流行的划分规则是模型-视图-控制模式(MVC),它规定模型(数据类),控制(程序的逻辑)和视图(图形用户界面)应该是splittet 分成不同的包。当我坚持这一点时,我会像这样划分我的代码。在每个包中,我都有不同的模型、视图和控制类,但是模型类不知道有关控制层的任何信息,并且控制层也不知道有关 GUI 的任何信息。

由于 GUI 开发总是乏味的,因此通常是应用程序中测试最少的部分(至少在单元测试中),因此将其与控件分开可以使编写业务逻辑变得更加容易。事实上,它可以让您专注于必须做的事情,即完成工作,并用业务逻辑进行编码。如果这部分有效,您可以花时间为其编写一个漂亮的 GUI。当然,GUI 和易用性常常会给控件带来自己的要求,但至少它是松散耦合的。

在我当前的大型项目中,我们有一些位组件,我们现在将其视为独立产品,并由真正的产品使用。这使得负责人更容易测试和编写独立组件,并为每个人提供了更稳定的组件。

只是我的2美分。

I will try to express my experience from previous projects here, maybe they help you.

If you try to segment your project, try to find components that might be useful as themselves. If you write e.g. a database layer, think about what to do to make the database layer independent from the rest of your application (except utility classes and configuration). If you write a RichClient that accesses the database layer, try to put exactly the stuff you need in it which you would also need when accessing it from a Web layer. Now you have a component that would maybe even useful in a command line client.

Or if you want to see this from a lower level, split your application into small units, and don't let these units have circular dependencies!!! If two components have circular dependencies, they cannot be splitted and should really be a single component. I know, I broke this rule some times because you cannot hold it up always, but it is a good rule to get understanding of the building blocks of your app.

Another popular dividing rule of an application is the Model-View-Control Pattern (MVC), which states that the Model (the data classes), the Control (the logic of your program) and the View (The graphical user interface) should be splittet into distinct packages. While I adhere to this, I would divide my code like this. Within each package I have distinct model, view and control classes, but the model classes don't know anything about the control layer, and the control layer does not know anything about the GUI.

Since GUI development is always tedious, and therefore often the least tested part (at least in unit tests) of an application, splitting it from the control make writing the buisiness logic a lot easier. In fact, it lets you concentrate on what you have to do, which is getting work done, which you code in buisiness logic. If this part works, you can spend time writing a nice GUI for it. Of course will the GUI and ease of use often bring their own requirements to the control, but at least its loosely coupled.

In my current large project we have some bit components, which we now see as independent products, which get used by the really product. This makes testing and writing the independent component easier for the person in charge, and gives everyone a more stable component.

Just my 2¢.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文