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.
A person invests a sum of money in two schemes, P and Q. Scheme P offers simple interest, and Scheme Q offers compound interest. The amount invested in ...
In each of the following questions, a question is followed by information given in three statements. You have to study the question along with the stat...
What is the capacity of a cylindrical tank?
I. The radius of the base is half of its height, which is 42 meters.
II. The area of the base ...
What is the profit%/loss% incurred by selling an article for Rs. 44,000?
Statement I: The difference between the cost price and the selling pric...
What is the value of number x ?
I. The HCF of x and 16 is 8.
II. The LCM of ...
There are 7 people in a group. Average age of the group decreases by 5 when two more people join the group. Find the average age of the new group.
<...Determine the time taken by a pipe R to empty a water tank.
Statement I: Pipe P and pipe Q can fill a water tank in 6 hou...
How much profit did the company earn in the year 2016?
I) The company earned 45% more profit in the year 2017 than that in the year 2015.
...
A box contains red and blue marbles. What is the ratio of red marbles to blue marbles in the box?
Statements I: Â The number of red marbles is 4...
How many students in a class can write both French and Spanish?
I. In the class, 25% of the total students can write Spanish and two-fifth of the...