Hello
Java Guru,
After reading the topic Advanced OO and Design
Patterns of the ebook oca-ocp-se-7-programmer 1 and 2. I went in for self questions but I had a problem with question 3 in page 567. Basically the question goes thus:
Given:
public class F {
private static final F f = new F();
public static F c() {
return f;
}
public void update(F a) { }
public void delete(F a) { }
}
Which design pattern or principle is implemented?
And the answer that was given as feedback is that this question implements the singleton design pattern.
As per my understanding for a class to implement a Singleton, 3 conditions are mandatory. Which are:
■ A private static variable to store the single instance called the singleton. This
variable is usually final to keep developers from accidentally changing it.
■ A public static method for callers to get a reference to the instance.
■ A private constructor so no callers can instantiate the object directly
I noticed that the class respect the first 2 conditions but not the last condition.
Why is the answer given as Singleton design pattern?
Besides, I also wanted to know if a Singleton design pattern implements the Object composition principles? Because in class we still have a field member as instance of the class.
Thanks for your response!