表单对象问题

发布于 2024-08-23 04:06:41 字数 173 浏览 12 评论 0原文

对于表单对象应该处理什么样的代码而不是让对象本身处理它,是否有人有任何硬性规定?例如,如果有一场比赛,正在比赛的对象应该说“马”,将比赛作为马的一部分来处理,还是最好将其放置在表单对象中?我想我要问的是如何决定什么进入像马这样的对象(如方法),以及什么进入表单对象而不是马。在这种情况下,您是否使用任何规则来确定代码的最佳抽象位置?

Does anyone have any hard and fast rules about what kind of code the form object should handle as opposed to letting the object itself handle it? For example, if there is a race, should the object that is racing say, horses, handle the race as part of being a horse, or is it better to place that inside the form object? I guess what I'm asking is how one decides what goes into an object like a horse as say a method, and what goes into a form object instead of a horse. Are there any rules you use to figure out where code is best abstracted in this case?

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

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

发布评论

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

评论(5

情绪少女 2024-08-30 04:06:41

这称为“关注点分离”。让表单处理显示和用户交互。让 Racer 处理赛车。

This is called "separation of concerns". Let the form handle the display and user interaction. Let the Racer handle racing.

摘星┃星的人 2024-08-30 04:06:41

我尝试开发我的软件,以便将不依赖于 UI 的核心功能抽象为负责其任务的类。

尝试思考:

我该如何编写这个代码,以便我可以同时拥有 GUI 界面和控制台界面,而无需重复任何代码。

UI 应该只处理视觉效果和视觉效果。用户交互。其他一切都应该根据其角色来组织。

I try to develop my software so that core functionality that is not UI dependent is abstracted into classes that bear responsibility for their tasks.

Try to think:

How could I write this so I could have both a GUI interface and a console interface without duplicating any code.

The UI should only handle visuals & user interaction. Everything else should be organized based on its role.

陌伤ぢ 2024-08-30 04:06:41

不确定这里有绝对正确的答案。但同意约翰·桑德斯的观点。 “表单”作业主要负责向用户显示数据并接受数据输入。你越接近这个目标,而且仅此而已。想想当有其他地方可以使用此类数据时,如果代码在其他地方,则可以重用。

让“业务对象”或“外观”处理竞赛的逻辑以及显示它的表单。

Not sure there is absolutely a right answer here. But agreed with John Saunders. A "Form's" job is primarily responsible to display data to the user and accept data input. The closer you keep it to that, and that alone. Think about when there's another place for this type of data to be used, if the code is elsewhere, it can be reused.

Have a "Business Object" or a "Facade" handle the logic of the race, and the form to display it.

焚却相思 2024-08-30 04:06:41

尝试以现实世界中的方式表示事物。任何描述马的属性或行为的内容都属于马对象。任何描述比赛属性或动作的内容(可能包括马对象的集合)都属于比赛对象。表单不是现实世界的对象,而只是用于显示马匹/比赛/其他任何信息的小工具。因此,除非需要在屏幕上显示真实数据,否则不要在表单中存储任何内容。

Try to represent things the way they are in the real world. Anything that describes the properties or actions of a horse, belongs in a horse object. Anything that describes the properties or actions of a race (including, perhaps, a collection of horse objects), belongs in a race object. A form is not a real world object, but just a gadget for displaying information from horses/races/whatever. So don't store anything with the form except as needed to present the real data on the screen.

鹿港巷口少年归 2024-08-30 04:06:41

由于表单是 UI 的一部分,我将应用我的 UI 硬性规则:
UI = 格式化、排序和显示数据以及接受和验证输入

Since the form is part of the UI I would apply my UI hard and fast rule:
UI = formating, sorting and displaying data plus accepting and verifying input

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