Page 1 of 1

About Question enthuware.ocpjp.v7.2.1187 :

Posted: Mon Apr 15, 2013 4:24 pm
by Ciprian Mihalache
I agree it is a very stupid question and the answer is (very) debatable.
But, in case I get a similar question on the exam, I am seriously considering to choose "getInstance" instead of "newXXX".

Your examples for "newXXX" factory methods are perfectly valid. I also noticed a lot of other "newXXX" in several other places:
- java.nio package:
+ FileSystems.getDefault().newWatchService();
+ several methods in Files class
+ several methods in Channels class
+ some other methods located in various classes in this package
- java.util.concurrent package (your examples + some other methods that create threads, tasks, lock conditions etc.)
- other places

I also agree that we are used to have "getInstance" method in a Singleton class. But, in case the "getInstance" method receives arguments, I believe it acts as a Factory. I want to illustrate this with your "Singleton example" : Currency.getInstance(Locale). If you look over the implementation, I believe you agree that it is a Factory; it does not provide a Singleton reference.
Maybe the best example is Calendar.getInstance(Locale). In the implementation there are various Calendar types (GregorianCalendar, BuddhistCalendar, JapaneseImperialCalendar), but it is hidden to the client the actual class that is being instantiated.
And there are plenty of other classes with "getInstance" methods that have arguments.

In fact neither Calendar.getInstance() is not returning a Singleton. This can be very easily spotted from the implementation or from the following code (the result is 2 different numbers so there are 2 different instances):

Code: Select all

Calendar instance1 = Calendar.getInstance();
Thread.sleep(100);
Calendar instance2 = Calendar.getInstance();
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
So, for sure both "getInstance" and "newXXX" are very strong candidates for the right answer. For me the greatest argument for selecting "newXXX" would be a testimonial of someone that scored 100% and choosed this answer. If you want to stay with "newXXX" as the right answer, please change the explanation for "getInstance" option, in order to indicate some real Singleton implementations.

Re: About Question enthuware.ocpjp.v7.2.1187 :

Posted: Mon Apr 15, 2013 6:32 pm
by admin
Hi Ciprian,
Your argument is very valid and we will add it to the explanation. As far as which answer is correct for the exam, it is anybody's guess.

thank you!
-Paul.

Re: About Question enthuware.ocpjp.v7.2.1187 :

Posted: Sun May 31, 2015 5:44 am
by rvdberg
As part of the SE II programmer exam:
RowsetFactory.createXXX()..

But okay, maybe createXXX() is the least used option..

Re: About Question enthuware.ocpjp.v7.2.1187 :

Posted: Thu Jul 12, 2018 11:07 am
by danidark9312
How do you know "getInstance" is not using singleton pattern ? ,how would one guess in the examn which clases uses singleton or factory when they call getInstance, because getInstance is part of the standar naming for singleton mehotd.