1

So I am just starting to look at the Play Framework and I see that in the examples (http://www.playframework.org/documentation/2.0.4/JavaTodoList) that everything seems to be static. I am one of those people though that believe that statics are not exactly a good thing and if you need a static you should probably be using a singleton. Can someone more familiar with the play framework please tell what the thought process was around all these static call's versus something more in line with the singleton pattern? I understand that I can implement any pattern I want after the initial static method call, but it seems to me that something else could have been done here to limit the use of static's. Again I am looking at the play framework for the first time today so I may be missing something, so feel free to enlighten me. Thank's.

peekay
  • 1,209
  • 2
  • 19
  • 46
  • duplicate question and answered by Guillaume Bort http://stackoverflow.com/questions/5192904/play-framework-uses-a-lot-of-statics – Schleichardt Dec 03 '12 at 20:45

1 Answers1

3

Play's approach is to be stateless, whereas non-static methods introduce state to a class.

The Singleton pattern does not really work there, either. With a Singleton, you have exactly one global instance of a class. This can have 4 possible outcomes, depending on your definitions (maybe more, but I cannot think of any):

  • If your Singleton instance is stateful and global means application-wide, then multiple visitors of the same page will share the same state ("Oh, look, I'm user X now. That's weird.")
  • If your Singleton instance is stateful and global means per HTTP request, then you just make a new "Singleton" and do not reuse it, rendering the Singleton pattern (in my opinion) useless.
  • If your Singleton instance is stateless, then why even bother with generating an instance of it and managing to have exactly one?

The fourth point would be a stateful Singleton, where global means per user. That does in fact result in a neat way to manage user sessions. But it is just not the way Play does it.

Carsten
  • 16,435
  • 3
  • 42
  • 51
  • I'm not sure I agree with the stateless singleton statement you make. I completely agree with utilizing a stateless approach, but I would argue that anytime a developer would want to use an object globally (regardless of global scope) that object should not maintain any state. obviously this would not be a hard and fast rule, more like a guide line. – peekay Dec 03 '12 at 16:26