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.
About Question enthuware.ocpjp.v7.2.1190 :
Moderator: admin
-
- Posts: 53
- Joined: Fri Sep 20, 2013 7:20 am
- Contact:
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1190 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Fri Sep 20, 2013 7:20 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1190 :
Ok Paul, understood, thanks.
-
- Posts: 78
- Joined: Sun Jun 30, 2013 10:04 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1190 :
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
http://www.gofpatterns.com/design-patte ... bility.php
-
- Posts: 1
- Joined: Mon Jun 09, 2014 2:36 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1190 :
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?
Or am I missing the plot here?
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?
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.Q: How many instances of a class that implements the singleton pattern can you create?
Or am I missing the plot here?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1190 :
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.
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.
Who is online
Users browsing this forum: No registered users and 217 guests