Singleton vs static


  • 2

    While coming across the singleton pattern, a common question that comes up is whether we need to take all that trouble to implement a singleton correctly (5 flavors of singleton) at all. Why not just have static methods and do away with singletons?

    public class SingletonSubstitute {
            private static State state = new State(); //static field init
    	public static void doWork() {
    		//do some work
    	}
    }
    

    Here are some reasons for preferring a singleton:

    1. Lazy initialization : We cannot get the benefits of lazy initialization if we use static init (either a static field init or a static block) as init will be performed as soon as the class is loaded.
    2. Polymorphism : The most important reason! Suppose we have to pass the singleton around as an object or suppose its a service that we get by performing a service lookup. In such scenarios, it is often the case that there is a need to implement a certain interface. Going with static leads to a dead end.
    3. Singleton implementation difficulty is overrated : Just roll with the enum implementation! It lowers code footprint and lends thread-safety and lazy-init right out of the box. Note: you can make enums implement interfaces as well!
    4. Lower semantic gap : If something consists of state, an object representation is always what is natural to use and understand.

  • 1
    J

    @soumyadeep2007 It is unclear what lazy initialization means. According to this post https://stackoverflow.com/questions/3499214/when-does-static-class-initialization-happen

    The static variables in a class are only initialized when any of the following happens:
    an instance of the class is created,
    a static method of the class is invoked,
    a static field of the class is assigned,
    a non-constant static field is used, or
    for a top-level class, an assert statement lexically nested within the class is executed.

    If none of the above happens, the static variables are not initialized. In other words, if the class is never touched, it is not initialized. Is this also some kind of "Lazy Initialization?"


  • 0

    @jz Great q!
    You may touch a singleton for a purpose which does not require its internal state, in which case the Singleton static variable instance will become initialized (not desired). For eg consider what will happen if you access a completely different field/method:

    public class SingletonSubstitute {
    	private static State state = new State(); //static field init
    	public static void doWork() {
    		//do some work..this handles and uses state
    	}
    	public static void sayHello() {
    		System.out.println("Hello"); //if you breakpoint here, you will see that state is already initialized (undesirable)
    	}
    }
    .
    .
    .
    SingletonSubstitute.sayHello();
    

    The sayHello method doesn't require the internal state but it is still initialized (you can verify with a debugger).

    Thus, the laziness that you are talking about isn't the actual laziness that is desired (which is laziness with respect to the state member).


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.