0

Recently I am working on an applications (in Java and C#, but I think the problem is not closed to those languages) where I use a few container classes (which responsibilities are storing data in a proper order) and services being method packages, operating on data stored in container classes. All of the classes mentioned above should have only one copy existing in memory, and all of them are expected to be in the memory for the whole time the application is running.

I used to think that a singleton is a good idea here, as I am sure there is only one instance of each class, so it meets my expectations. However, I learned that the Singleton pattern is deprecated, as it hides dependencies and so on. Then I heard that for such usage (always available container class or method package) static classes may be a good idea. On the other hand I recently looked at a few projects where people refused to use any static stuff, as if it was an awful practice to do so.

My question is simple (at least in its formula): are static classes a good idea for creating always available, easy to hanlde containers and method packages? If not, what should I use instead (if not singletons)?

3yakuya
  • 2,471
  • 3
  • 20
  • 38
  • 2
    No such thing as a 'static class' in Java: There are only classes with static members. The number 1 reason to avoid `static` is that it makes your code harder to test. There is no way to pass in "test doubles" to a class or a method that uses static fields and calls static methods. Google for "test double" if you aren't familiar with the concept. – Solomon Slow Sep 12 '14 at 20:09
  • 1
    Looking at it from architectural point of view, I thought you can assume a class with all its members being static can be called a static class, like it is in C#. All of its contents are reachable from the static context - I think it makes it static. – 3yakuya Sep 12 '14 at 20:11

1 Answers1

1

You don't really say where the data comes from. If the data is static, then a static class is a fine solution. For example, I could envision a static class to represent the 50 US states.

In contrast, for a class that represents a list of authorized users, I would use a singleton pattern. Although there is only 1 list, that list could change while the app is running.

KC-NH
  • 748
  • 3
  • 6
  • The data is not static in the use I intend. How about the deprecation of Singleton here - is it ok in some cases, or is my whole idea for Architecture wrong? – 3yakuya Sep 12 '14 at 20:20
  • 2
    What deprecation of Singleton? I must have missed the memo. It's perfectly appropriate for data where there is only one instance. – KC-NH Sep 12 '14 at 20:26
  • http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons Got info form here, among some others. Do I take it wrong – 3yakuya Sep 12 '14 at 20:38
  • I came to a conclusion that static classes are OK for "really static" things, like method packages, that contain fixed methods. For data containers, which may contain different objects etc. during runtime I'd go for Singletons, watching out to pass it arround properly. Thank you. – 3yakuya Sep 13 '14 at 12:53