I am currently learning about the Singleton pattern. I learnt that the classic way to implement it is to create a static field of the Singleton class type, hide the constructor using private access modifier, and provide a public getInstance()
method.
However, I thought of another way of implementing it without using private constructors:
public class SWrapper {
private static Singleton holder = new Singleton();
private static class Singleton{ /* implementation without private constructor*/}
public static Singleton getInstance() {
return holder;
}
QUESTION: Does this implementation work? (I think it does but I can't be sure.) If it does, are there any advantages or disadvantages to using this implementation?
Aucun commentaire:
Enregistrer un commentaire