About Question enthuware.ocpjp.v7.2.1190 :
Posted: Fri Sep 20, 2013 8:20 am
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.
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.