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 sum of the cost prices of two articles, 'C' and 'D', is Rs. 480. Both articles are marked up by 60%, whereas the marked price of article 'D' is Rs. ...
A bought a laptop for Rs.25000. He spent 22% of the amount that he had paid for buying it for its repair. He then sold the laptop to B and earned a prof...
On selling 80 articles for Rs. 6000 there is a loss equal to the cost price of 20 articles. Find the selling price of 40 articles if the shopkeeper want...
Cost price of a bag is Rs.700. The shopkeeper marked it 80% above the cost price and sold it after giving a discount of 25%. If the shopkeeper had sold ...
Ravi purchased an article and marked it 75% above the cost price and sold it after giving a discount of 25% for Rs. 1575. What will be the selling price...
The marked price of item A is Rs. 90 less than the selling price of item B. The marked price of item B is 80% more than the selling price of item A. If ...
A shopkeeper sold an article after offering two successive discounts of 21% and 25%, respectively. The marked price of the article is Rs. 2300. If the c...
A shopkeeper buys 20 articles for Rs 5,000. He sells each article at 25% profit on the cost price. Due to a seasonal sale, he offers a 10% discount on t...
'A' sold an article whose cost price is Rs. 'V', at a profit of 10% to 'B'. 'B' marked the price of the article 10% above the price at which he bought i...
Zara purchased a microwave for Rs. 2400 and spent some amount on service. She marked it up by 25%, gave a Rs. 360 discount, and earned a 15% profit. Fin...