Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use lazy Singletons over normal Singletons?

So far I have seen two examples of Singletons.

Normal Singletons,

public class Singleton {
  private static Singleton instance;

  static {
    instance = new Singleton();
  }

  private Singleton() { 
    // hidden constructor
  }    

  public static Singleton getInstance() {
    return instance;
  }
}

and Lazy Singletons,

public class Singleton {

  private Singleton() { 
    // hidden constructor
  }

  private static class Holder {
    static final Singleton INSTANCE = new Singleton();
  }

  public static Singleton getInstance() {
    return Holder.INSTANCE;
  }
}

Coding is from this thread and this user. I have just recently gotten into trying to learn Singletons as my previous methods have been

1.) Using static in order to create something like ...

static MyClass instance;

2.) I would attempt to pass an instance in a seemingly odd way,

MyClass instance;

@Override
public void onEnable() { instance = this; }

// Transition to different class - - -

public OtherClass(MyClass myClass) {
    this.instance = myClass;
}

Lastly, what is my end goal? I am mainly using it in order to pass variables from my main class to other classes. I'm currently attempting to learn how to properly use Files and FileConfiguration, so I want to easily share them throughout my classes.

If I seem like a beginner, instead of going out of your way to tell me to learn Java, please provide a resource to help me with my problem first and foremost.

like image 835
VeeAyeInIn Avatar asked Jan 27 '18 00:01

VeeAyeInIn


2 Answers

As to when, rather than how: I would use lazy instantiation of a singleton or of any other object when there is a fair chance of the object not being needed, and immediate instantiation when the likelihood of it being needed is high. In general, if instantiation were to fail, and the object is needed, it is better that it fail as early as possible.

like image 94
Jonathan Rosenne Avatar answered Oct 20 '22 19:10

Jonathan Rosenne


This link explains it fairly well and even uses a similar example.

In software engineering, the initialization-on-demand holder (design pattern) idiom is a lazy-loaded singleton. In all versions of Java, the idiom enables a safe, highly concurrent lazy initialization with good performance.

Regarding why you should use this: if the creation of this instance is expensive, then this design pattern essentially delegates the expensive computation for when it is needed, rather than when the outer class, Singleton in your case, is first accessed.

Another reason is given by this other link. It states:

A singleton implementation may use lazy initialization, where the instance is created when the static method is first invoked. If the static method might be called from multiple threads simultaneously, measures may need to be taken to prevent race conditions that could result in the creation of multiple instances of the class.

like image 45
Jacob G. Avatar answered Oct 20 '22 17:10

Jacob G.