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.
I hope to make up (A)/athletics and would like to (B)/compete in either the (C)/100 metre or 200 metre sprint (D).
Each sentence given below has been divided into four Sentence A, B, C, D, one of which is erroneous. Identify the erroneous part and mark that option ...
Inflation figures based on (A) /the Consumer Price Index (CPI) shows (B)/Â retail price gains at (C) / an resurgent trajectory with (D) /Â food prices...
Read the given sentence to find out whether there are any grammatical/contextual errors in it. The errors, if any, will be in two of the phrases of the ...
Spot the grammatical errors in the given sentence. Mark the part with error as your answer. If there is no error, mark "No error" as the answer. (Ignor...
Identify the segment in the sentence, which contains the grammatical error
Nikhil is more braver than Sushant who is afraid of the dark.
Were I the President(A)/ I would award(B)/ you a title.(C)/ No error(D)
A nation what had been once tried to kill him was the first to embrace him.
One of the lesson that (A)/ we must learn from dealing (B)/ with the outbreak is the importance (C)/ of data management (D)/ for authorities in the city...
In the following question, some part of the sentence may have errors. Find out which part of the sentence has an error and select the appropriate option...