Which design pattern best describes the class?

Given:
<code>
public class DataCache {
private static final DataCache instance = new DataCache ();
public static DataCache getInstance () {
return instance;
}
}
</code>

Which design pattern best describes the class?

Given:
<code>
public class DataCache {
private static final DataCache instance = new DataCache ();
public static DataCache getInstance () {
return instance;
}
}
</code>

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 classs 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.



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Marian

Marian

C

ahmed abbas

ahmed abbas

answer is ::A

it’s a singlton

Vins

Vins

A, but constructor should be private