Given:
public class DataCache {
private static final DataCache instance = new DataCache ();
public static DataCache getInstance () {
return instance;
}
}
Which design pattern best describes the class?
A.
Singleton
B.
DAO
C.
Abstract Factory
D.
Composition
Explanation:
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.