什么类型的方法最适合作为“静态”存在?在 MVC 中?
在 MVC 模式中定义方法的访问控制的最佳实践是什么。我不确定在 ATM(客户端-服务器模型)等场景中在哪里使用静态方法。我正在使用Java。
如果有人能阐明这一点,我将不胜感激。
这是我用来绘制类图的方法。
- 首先,我设计了所有的屏幕,并在纸上绘制草图。 (我有点 艺术家,如果你想知道并喜欢图形设计的东西:))
- 然后我通过研究场景创建了视图类创建了模型类 并使用数据
- 为每个模型创建控制器以及其他一些控制器
- 通过查看屏幕中的按钮向控制器添加方法,我认为这是一种永远不会错过任何方法的直接方法? +一些额外的GUI控制 等等。
你觉得我的方法怎么样?
谢谢。
What are best practices in defining access controls for methods in MVC pattern. I'm uncertain where to use static methods in a scenario like an ATM (client-server model). I'm using Java.
Appreciate if someone can shed some light on this.
Here's my approach I used to come up with a class diagram.
- First I designed all the screens, sketched on a paper. (I'm kinda
artist if you wonder and into graphic design stuff :) ) - Then I created View classes Created Model classes by studying the scenario
and use of data - Created Controllers for each Model and some more additional ones
- Added methods to Controllers by looking at the buttons I got in the screens, which I think a straight forward way to never miss any method? + some additional ones for GUI controlling
etc.
What do you think about my approach?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白 MVC 与它有什么关系。静态方法无法访问实例变量。因此,静态只能用于通过参数接收所有必要数据的方法。通常这是“实用”例程,例如排序例程、格式化程序、通用计算等。
您可能使用静态例程的另一个地方是访问类中的静态变量。但您很少希望在不访问实例变量的情况下执行此操作。
请注意,没有真正的要求将任何例程设为静态 - 您可以拥有不引用任何实例变量的实例方法。但是,在您没有方便的实例来调用该方法的情况下,可以访问静态方法。
I don't see what MVC has to do with it. A static method cannot access instance variables. Therefore static should only be used for methods that receive ALL their necessary data via parameters. Usually this is "utility" routines such as sort routines, formatters, common calculations, etc.
The other place where you might use static routines is to access static variables in a class. But it's rare that you want to do this without also accessing instance variables.
Note that there's no real requirement to make any routine static -- you can have an instance method that doesn't reference any instance variables. But a static method becomes accessible from situations where you don't have an instance handy to invoke the method.