DEV Community

Effective Java Tuesday! Singletons!

Kyle Carter on October 01, 2019

Time for chapter three of our Effective Java review. Today's is a fairly simple one. Today we are talking about the singleton pattern. The singleto...
Collapse
 
jboschmans profile image
jboschmans

I know I'm late to the party, but I wonder why you would ever use the Singleton pattern instead of just a static class. Doesn't it achieve exactly the same?

Collapse
 
kylec32 profile image
Kyle Carter

I'm not sure I'm following what you mean by "static class." The only static classes come to mind are static inner classes (dev.to/kylec32/effective-java-favo...) which don't solve the singleton problem but instead allows access to a nested class without having a handle on the outer class. Still useful, just not for this problem. Maybe I'm just misunderstanding the question thought.

Collapse
 
jboschmans profile image
jboschmans

Thank you for your response. I guess I didn't explain very well. What I mean is a class where the members and methods are static. This way you can call the static method of the class to do what you want instead of creating an object. eg: ClassName.staticMethod()

Thread Thread
 
kylec32 profile image
Kyle Carter

Thanks for that clarification. Yeah I don't see any problem with that. It seems like just a slight change from option 1. Potentially the downside I see is you don't keep the option of changing how it gets initialized in the future. If that's not needed then you likely are good with this option.

Thread Thread
 
jboschmans profile image
jboschmans

Thank you for your answer!

Collapse
 
raipc profile image
Anton Rybochkin

What about storing the singleton value in a static nested class?

Collapse
 
kylec32 profile image
Kyle Carter

Interesting. I see no reason that you couldn't do that if it made sense to put the singleton in such a nested class. Something to keep in mind if the option ever comes up.

Collapse
 
raipc profile image
Anton Rybochkin

I thought that was the official way to create lazy singletons without synchronization.

public class CEO {
  private CEO() { }

  public static CEO getInstance() {
    return InstanceHolder.instance;
  }

  public void fire(Employee slacker) { ... }

  private static final class InstanceHolder {
    private static final CEO instance = new CEO();
  }
}
Thread Thread
 
kylec32 profile image
Kyle Carter

Will you look at that! Still never seen that but you are 100% correct this would allow lazy initialization without synchronization. Thanks for educating me about that.

Collapse
 
itsraghz profile image
Raghavan alias Saravanan Muthu

Wow.. this is pretty awesome series. Gonna bookmark all the posts in this thread/series and start grasping them gradually. Thank you Kyle.

Collapse
 
rossholloway94 profile image
Ross Holloway

"They are extremely hard to test" - why?

Collapse
 
kylec32 profile image
Kyle Carter • Edited

A fair question indeed, and one that I did not demonstrate above in my examples. Since the constructor is not accessible to us, even in a test, we are unable to change how the object is created and can't get the benefit of dependency injection (dev.to/kylec32/effective-java-tues...). While none of the above examples show any places where that would be a problem, many singletons in the wild do have this problem if they have any external dependencies (DB, network, other components, etc). So the more I think of it, it's likely the lack of ability to use dependency injection that is the true problem here.

Compare that to a singleton that is just a collection of pure functions that would indeed be testable (but at that point you are more looking at a Utility class dev.to/kylec32/effective-java-tues...)

Collapse
 
rossholloway94 profile image
Ross Holloway • Edited

I think you're right about Dependency Injection for unit testing with only, say, JUnit 5.

However, this is where I have found mocking libraries such as Mockito benefit the tests, as the coupled logic can be stubbed out.

(Thanks for the detailed reply 👍 )

Edit: Your second link doesn't appear to be working?

Collapse
 
ehimsi profile image
Himanshu Joshi

Per Effective Java - we get the serialization machinery free.