0

If singletons are considered bad for global state, particularly state that can affects the main execution of code, then how should global state for, say, a web framework be handled?

The immediate things that come to mind are:

  • Base Url
  • Base File Path
  • General configs
  • Logging instance
  • etc

I cant see any other way than a singleton for access to these such as an App() class or similar?

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • 4
    Where did you get the reference that singletons are considered bad for global state? – Marcelo Aug 09 '13 at 21:39
  • I was under the impression this was a general consensus among developers? I must be wrong then - in which case i will use them more! Thanks :) – Marty Wallace Aug 09 '13 at 21:46
  • @MartyWallace the real problem is not about using a singleton or not, it's *why* you think you may need a singleton. – Luiggi Mendoza Aug 09 '13 at 21:47
  • @Marcelo - singletons are per class-loader so if an app is using more than one class-loader keeping the global state in a singleton may be problematic. – pl47ypus Aug 09 '13 at 21:47
  • @pl47ypus In what cases do you normally use one than more class-loader? – Marcelo Aug 09 '13 at 21:49
  • @Marcelo http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – assylias Aug 09 '13 at 21:50
  • @LuiggiMendoza - so that is the crux of my question. How would i go about handling the state i mentioned without using a singleton? – Marty Wallace Aug 09 '13 at 21:52
  • 2
    It's Global State which is "the real bad boy. Wikipedia cites William Wulf and Mary Shaw, “Global Variable Considered Harmful”, ACM SIGPLAN Notices, volume 8, issue 2, 1973 February, pp. 28–34. I don't think there's been any serious argument in the last forty years. Singletons just add unnecessary complication on top of global state. – Tom Hawtin - tackline Aug 09 '13 at 21:52
  • 1
    @Marcelo - "Sometimes in Java you need to load a set of classes that are isolated from the classpath. For instance, you might need to support the capability to plugin a library to your main application and you want to be able to isolate the libraries used by the plugins from your main application." the rest is here: http://tech.puredanger.com/2006/11/09/classloader/ – pl47ypus Aug 09 '13 at 21:55
  • https://twitter.com/codemonkey_uk/status/157463012284960769 – Tom Hawtin - tackline Aug 09 '13 at 21:58
  • @pl47ypus I would like to see a real world application of this in a web application. – Luiggi Mendoza Aug 09 '13 at 22:03
  • @LuiggiMendoza - i'm actually working on such one (we serve hundreds of millions requests and process 3-5 TB of new data every day). – pl47ypus Aug 09 '13 at 22:07

1 Answers1

5

Load this data in a place that is already global for your application. For instance, in Web applications, you can store this data in application scope i.e. ServletContext using a ServletContextListener.

Also, you can use a framework that already handles a global state per certain context like Spring.

By the way, there are cases like Logging instances (i.e. Logger logger) that aren't stored in singleton instances but in each class, marked as static final.

More info related to the long-never-endind discussion about Singleton pattern usage:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332