Play! framework uses a of statics

后端 未结 14 1870
太阳男子
太阳男子 2020-12-07 15:29

Waaah, the Play! framework has so many static methods. Where I go to school, we were told never ever to use any statics, yet Play! uses it like there\'s no tomorrow

相关标签:
14条回答
  • 2020-12-07 16:18

    Statics method in play are mainly used in controllers action methods. These methods are meant to just fetch the necesary data from the model and expose it to views.

    They correspond somehow to each possible http request, and, just like those http request are completely stateless.

    On structural programming you have procedures on the one hand, and variables on the other, but on OOP paradigm you treat procedures and variables as a whole.

    That is, you have and object with instance methods (procedures) and instance variables.

    But controller actions are stateless, that is they get all there variables from the request (maybe also from the cache, but in that case you need some sort of session id that finally comes from the request). So controller actions are just like stateles procedures, and that's why they don't particularly fit in the OOP paradigm, as models do.

    0 讨论(0)
  • 2020-12-07 16:19

    As with anything in programming, never ever is never the right answer. Just like always. There are always exceptions and the right answer is always 'it depends'.

    It's true that in pure OO (which I'm all for) there is very little room for statics. But it's also true that sometimes they just make sense.

    The classic example is utility methods. Sure, it would be better if we could just append our abs() method to Integer. But we can't; so we're stuck with Math.abs(int i).

    I tend to think it's just correct to make a method static when it has nothing to do with the instance itself. For instance, in a class Person, you could have a method that takes a list of people, and returns the number of people that have a birthday today. Maybe you can only do this in the class itself if the data needed to do the calculation is private (something an OO purist would understand ;)) but still the method clearly has no relation to a single Person instance.

    Another thing is internal classes. You often want to make them static if you don't need the relation with the containing type.

    I've never seen Play! but if you say that over 50% of it is static, then I'm guessing it was probably badly designed. That's no exception; a lot of frameworks are. Don't let it get you down. Definately don't learn from it!
    But if it works you can still use it.

    0 讨论(0)
提交回复
热议问题