Which two properly implement a Singleton pattern?
A.
class Singleton {
private static Singleton instance;
private Singleton () {}
public static synchronized Singleton getInstance() {
if (instance = = null) {
instance = new Singleton ();
}
return instance;
}
}
B.
class Singleton {
private static Singleton instance = new Singleton();
protected Singleton () {}
public static Singleton getInstance () {
return Instance;
}
}
C.
class Singleton {
Singleton () {}
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton (); }
public static Singleton getInstance () {
return SingletonHolder.INSTANCE;
}
}
D.
enum Singleton {
INSTANCE;
}
Explanation:
A: Here the method for getting the reference to the SingleTon object is correct.
B: The constructor should be private such as:private static Singleton instance = new Singleton();
Note: Java has several design patterns Singleton Pattern being the most commonly used. Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM.
The class’s default constructor is made private, which prevents the direct instantiation of the object by others (Other Classes). A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.
A and B
Should be B and D as A blocks another call when getInstance() is processed.
Correct Answer is C and D. B is incorrect becoz any class extends Singleton Class it creates an instance so its violating the pattern.
A is incorrect becoz if two threads are waiting each other(consider the race condition-Thread interference) the instance field modified by one thread is not visible to other thread.(If it uses volatile keyword then it makes sense)…. Best pratices for singleton is using Enum. C is correct becoz SingletonHolder is loaded on the first execution of Singleton.getInstance() or the first access to SingletonHolder.INSTANCE, not before.
A, because the code implements the concept of lazy initialization — meaning, you are creating the object of the singleton class, only when the getInstance() method is invoked. It also means that if the getInstance() method is not invoked, then the instance shall never be created.
All others are incorrect!!
The single-element enum type solution (from “Effective Java” 2nd edition by Joshua Bloch):
public enum Singleton {
INSTANCE;
}
Answers== A & D
OPTION A == SHOW THE LAZY initialization WITHOUT DOUBLE CHECKED LOCKING TECHNIQUE ,BUT ITS CORRECT
OPTION D == Serialzation and thraead-safety guaranteed and with couple of line of code enum Singleton pattern is best way to create Singleton in Java 5 world.
AND THERE ARE 5 WAY TO CREATE SINGLETON CLASS IN JAVA
1>>LAZY LOADING (initialization) USING SYCHRONIZATION
2>>CLASS LOADING (initialization) USING
private static final Singleton instance = new Singleton();
3>>USING ENUM
4>>USING STATIC NESTED CLASS
5>>USING STATIC BLOCK
AND MAKE CONSTRUCTOR PRIVATE IN ALL 5 WAY.
+1
A, D
B & C shall change constructor to private
for more details, refer to the link
http://www.shaunabram.com/singleton-implementations/
A and D
A,D
A D
Singleton constructor should be private
A and D
a,d