About Question enthuware.ocpjp.v7.2.1190 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Student
Posts: 53
Joined: Fri Sep 20, 2013 7:20 am
Contact:

About Question enthuware.ocpjp.v7.2.1190 :

Post by Student »

Q: How many instances of a class that implements the singleton pattern can you create?
A: 1. The goal of the singleton pattern is make sure that only one instance of the class is created.

Is it not the case that it is very difficult to ensure only one instance of a class is created, because of issues with multi-threading? I have in my notes something on this from the Ganesh/Sharma book:

"It is really important (as well as really difficult) to ensure that your singleton pattern implementation allows only instance of the class. In the case of multiple threads, trying to get a singleton object may result in creation of multiple objects, which of course defeats the purpose of implementing a singleton."

Hence I answered "Depends on how it is configured" because it was the closest answer to, "well, we can't say because we don't know if the Singleton class was correctly coded to take account of multi-threading issues".

Thanks.

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, you may not know how the class is coded but theoretically, when you talk about Singleton pattern, you want only a single instance of that class. If your class allows multiple instances, then, ideally, it is not following the Singleton pattern.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Student
Posts: 53
Joined: Fri Sep 20, 2013 7:20 am
Contact:

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

Post by Student »

Ok Paul, understood, thanks.

ewebxml
Posts: 78
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post by ewebxml »

The class should have exactly one instance which should be accessible from a method in the class.

http://www.gofpatterns.com/design-patte ... bility.php

dowsieboi
Posts: 1
Joined: Mon Jun 09, 2014 2:36 am
Contact:

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

Post by dowsieboi »

Not sure if people still read these old questions :)

Anyway, so the way I understand the Singleton pattern is that only 1 instance is created and you access that single reference each time but is this question not just a lil' ambiguous in terms of the creation of that instance?
Q: How many instances of a class that implements the singleton pattern can you create?
Technically "you" don't create any instances as the instance could be created when the class loads and "you" will only ever access the reference? So if it's done on class load then technically the answer would be 0? Unless you do it as a check in the getInstance method and create an instance if there isn't already 1? Which would then technically mean "you" are then creating an instance.

Or am I missing the plot here?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Well, you here doesn't really mean you or any specific person but in general.

Also, technically, you do create a new instance, not using the new keyword but by causing the class to load. It is like saying how much gas will you consume if the drive from point A to B. Of course, you don't consume the gas, it is the car that consumes it.

But I do agree with your point that it could be a little bit of ambiguous and it has now been changed to make it more clear.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 217 guests