Question
Which design pattern is best suited for ensuring that a
class has only one instance and provides a global point of access to it?Solution
The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it. It is often used for managing shared resources like a database connection pool, logging, or configuration settings. This pattern works by restricting the class's instantiation and providing a static method to get the single instance. Singleton is particularly useful when exactly one object is required to coordinate actions across the system. This pattern is implemented by making the constructor private, storing the single instance as a static property, and providing a public static method to access it. It ensures thread safety and lazy initialization in multi-threaded applications when implemented carefully. Explanation of Incorrect Options: A) Factory Pattern : The Factory Pattern focuses on creating objects without specifying their exact classes. It abstracts the instantiation process, allowing for flexibility in object creation. However, it does not restrict the number of instances or provide a global access point, making it unsuitable for the problem described. C) Observer Pattern : The Observer Pattern defines a one-to-many dependency between objects, where changes in one object are automatically reflected in dependent objects. It is ideal for event-driven systems but does not restrict the number of instances or provide global access, making it incorrect. D) Strategy Pattern : The Strategy Pattern allows behavior to be selected at runtime by encapsulating algorithms in separate classes. While it promotes flexibility, it does not deal with instance control or global access, making it irrelevant to the Singleton problem. E) Prototype Pattern : The Prototype Pattern involves creating new objects by copying existing ones rather than instantiating new ones. It is useful for object duplication but does not restrict the number of instances, hence it is not applicable here.
In the following question, a group of letters is given. The letters of each group are related with the letters of the same group by some Logic/Rule/Rela...
What will come in the place of β?β in the following equation, if β+β and βΓ·β are interchanged and βΓβ and βββ are interchanged?<...
Three of the following four are alike in a certain way and thus form a group. Which is the one that does NOT belong to that group?
(NOTE: Operati...
Select the Venn diagram that best illustrates the relationship among the following classes.
Females, Relatives, Uncles
Which of the following letter-clusters will replace the question mark (?) in the given series to make it logically complete?
ZXV, TRP, NLJ, HFD, ?
A, B, C, D, E, F and G are sitting in a straight line facing north. C sits third to the right of B. A sits second to the right of C. G is an immediate n...
Ramdin started from his office and walked 15 km in the south direction and reached the bus stop, and then turned to his left and walked 8 km straight on...
If 12th Dec 2009 is a Saturday, then what will be the day of the week on 16th Dec 2014?
10 is related to 15 following certain logic. Following the same logic, 42 is related to 63. To which of the following numbers is 22 related, following t...
What should come in place of? in the given series based on the English alphabetical order?
UDM, XGP, AJS, DMV, ?