简体   繁体   English

执行UI时如何设计类?

[英]How do i design classes when doing UI?

I notice a pattern, when i did C++ and backend programming (in C# or any language) all my classes are neat and tidy. 我注意到一种模式,当我进行C ++和后端编程(使用C#或任何语言)时,我所有的类都整齐而整洁。 Recently i notice all my code are in a class and i literally have >50functions in it. 最近,我注意到我所有的代码都在一个类中,并且我实际上有> 50个函数。 I now realize its because i am doing UI. 我现在意识到了,因为我正在做UI。 If i were to separate them by pages or forms/dialogs i would have a LOT MORE files, more lines of code and longer line of code. 如果我要按页面或表单/对话框将它们分开,则我将拥有更多文件,更多代码行和更长代码行。 If i separate them i get the same problem (more files, lines, longer lines). 如果我将它们分开,则会遇到相同的问题(更多的文件,更多的行,更长的行)。 Obviously the less lines the better (less code = less to debug, change or break during maintenance). 显然,行越少越好(代码越少=维护期间调试,更改或破坏的次数越少)。

This specific project is 5k lines with 2k being from the web or libraries. 这个特定的项目是5k行,其中2k来自网络或库。 All my .cs files are <1k lines. 我所有的.cs文件都是<1k行。 Is this acceptable even though i have 50+functions in a single class? 即使我在一堂课中有50多个功能,这也可以接受吗?

Bonus: I notice most of these functions are called only once. 奖励:我注意到其中大多数功能仅被调用一次。 and putting certain code blocks (such as one function make two calls to the db) as their own function makes it harder for me to edit since they are divide between files and this balloon function count. 并将某些代码块(例如一个函数对数据库进行两次调用)作为它们自己的函数,这使我更难编辑,因为它们是在文件之间分配的,并且气球功能计数也是如此。 So, i kind of dont know what to do. 所以,我有点不知道该怎么办。 Do i create more classes to reduce function count (per class, it will increase function calls overall and already most are only called once)? 我是否创建更多的类来减少函数数量(每个类将增加总体上的函数调用,并且大多数已经被调用一次)? How do i design classes doing frontend/UI? 我如何设计做前端/ UI的类?

I often find that my UI stuff grows considerably more complex than pure classes. 我经常发现我的UI东西变得比纯类复杂得多。 Think about it - your "pure" classes are (for the most part) essentially machine instructions, and can (or should be able to) assume pure, pre-validated inputs and outputs, and do not have to accomodate the vagaries of human behavior. 考虑一下-您的“纯”类(基本上)基本上是机器指令,并且可以(或应该能够)假定经过预先验证的纯输入和输出,而不必适应人类行为的变化。

A UI, on the other hand, is subject to all of human fallacy - and needs to respond in human-predictable ways, in a manner which humans can understand. 另一方面,UI会遭受所有人类的谬论-并且需要以人类可以理解的方式以人类可预测的方式做出响应。 THIS is where the complexity comes in. 这就是复杂性的所在。

Consider - in your nice, crisply defined classes, in which each function performs a fixed action against a known type of input, there is not alot of random BS to anticipate or handle. 考虑一下-在漂亮的,清晰定义的类中,其中每个函数针对已知的输入类型执行固定的操作,因此没有很多随机的BS可以预期或处理。

A UI must be receptive to all manner of improper, inconsistent, or unanticipated input and actions by the user. UI必须接受用户的所有不当,不一致或意外的输入和动作。 While we, as designers, can pre-think some of this (and even minimize it with things like Combo-boxes and Command Buttons, a). 虽然我们作为设计师可以预先考虑其中的一些(甚至可以通过组合框和命令按钮将其最小化,a)。 all of that requires additional back-side code, and b) all of these things can then interact in different ways as well. 所有这些都需要附加的后端代码,并且b)所有这些事情然后也可以以不同的方式进行交互。

In our classes, WE decide how certain methods/functions and the like interact and affect one another. 在我们的课程中,WE决定某些方法/功能等如何相互作用并相互影响。 on the UI, we can do our best to point the user in the right direction, but there is still the random element. 在用户界面上,我们可以尽最大努力将用户指向正确的方向,但是仍然存在随机元素。 What if the user pushes the button before selecting an item from the list? 如果用户在从列表中选择一个项目之前按下了按钮怎么办? There are several different ways to handle that scenario, all of which require another line (or ten, or 100) of code to handle gracefully. 有几种不同的方法可以处理这种情况,所有这些都需要另一行(或十或一百)代码来优雅地处理。

Lastly, the more complex the project, the more complex the UI is likely to be, and the more of this must go on. 最后,项目越复杂,UI可能越复杂,并且必须进行的工作越多。

Managing the actions of the machine, given inputs and outpus we as programmers explicitly define, is EASY compared to predicting, managing, and handling the random quirks imposed on our stuff by a user. 相较于预测,管理和处理用户强加给我们的东西的随机怪癖,管理程序员在程序员明确定义的给定输入和产出的情况下的动作非常容易。 If only they would pay attention, right? 如果他们能引起注意,对吗?

Anyway, I believe all of THAT is why code for a UI becomes greater, and more complex. 无论如何,我相信所有这就是为什么UI代码变得更大,更复杂的原因。 As for how to break it out in a maintanable manner, the guys above covered it. 至于如何以可维护的方式将其分解,上述人员将其覆盖。 Abstract the two. 抽象两个。 I design a form. 我设计一个表格。 I define the menas for the user to input data, and/or indicate what they want to happen next. 我定义了供用户输入数据和/或指出他们接下来要发生什么的菜单。 Then I define the manner in which that form can communicate those things with my crisp, clean back end classes. 然后,我定义该表单与我的干净整洁的后端类交流这些内容的方式。 Then I provide validation mechanisms, and the means to help control the user in navigating it all (eg the button is not enabled until the user selects an item from the list . . .). 然后,我提供了验证机制,以及帮助控制用户浏览所有内容的方法(例如,直到用户从列表中选择一个项目之前,按钮才处于启用状态……)。

Complex. 复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM