开发者

What types of methods are most suitable to exist as "static" in MVC?

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.

  1. First I designed all the screens, sketched on a paper. (I'm kinda artist if you wonder and into graphic design stuff :) )
  2. Then I created View classes Created Model classes by studying the scenario and use of data
  3. Created Controllers for each Model and some more additional ones
  4. 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜