mardi 29 novembre 2016

Singleton Pattern Recommended Method

Case 1:

public class Singleton {

  public static final Singleton INSTANCE = new Singleton();

  private Singleton() {
    ...
  }
}

Case 2:

public class Singleton {

  private static final Singleton INSTANCE = new Singleton();

  private Singleton() {
    ...
  }

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

Is the second method the recommended way to implement the Singleton design pattern, because I have never seen the first one in any example for Singleton pattern.

Aucun commentaire:

Enregistrer un commentaire