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.
The ratio of length to breadth of a rectangle is 5:4, respectively. Perimeter of rectangle is 306 cm. If each side of a square is 25% more than the brea...
Pankaj Advani bagged his 41st title by defeating whom, to lift his 8th title at the Asian 100 UP Billiards Championship 2022?
Refer to the following number series and answer the question:
5 1 2 6 3 8 6 5 3 6 5 3 2 5 6 9 5 1 7 2
How many such numbers are there in t...
The product of two positive numbers is 3920. If one number is five times the other, then the sum of the two numbers is:
The income ratio of Aman to Qureshi is 5:7. Both individuals save ₹900 each. If the ratio of their expenditures is 7:11, determine Qureshi's total exp...
In the following number-pairs, the second number is obtained by applying certain mathematical operations to the first number. Which numbers should repla...
Naomi Osaka won the women's singles title of the Australian Open Tennis Tournament, 2021. Who among the following was defeated by her in the finals?
A series of compounds in which the same functional group substitutes for hydrogen in a carbon chain is called __________.
Planets do not twinkle because:
Journal lists transactions in which order?