Question
Which design pattern would be most appropriate to ensure
that only one instance of a class exists and provides a global point of access to that instance?ÂSolution
The Singleton Pattern is a design pattern used to restrict a class to only one instance, ensuring that the same instance is globally accessible across the application. This pattern is particularly useful in cases like logging, configuration management, or database connections, where multiple instances could lead to resource conflicts or inefficiencies. The Singleton Pattern is typically implemented by making the class constructor private, defining a static method for instance retrieval, and storing the single instance in a static variable. This setup ensures that the instance is created only once and reused throughout the application, conserving resources and maintaining consistency by providing a unified point of interaction. Option A (Factory Pattern) - The Factory Pattern is focused on creating objects based on input parameters without limiting the number of instances, unlike Singleton, which restricts instantiation. Option C (Observer Pattern) - Observer Pattern establishes a dependency between objects, allowing them to notify others of changes, differing from Singleton’s focus on single-instance enforcement. Option D (Strategy Pattern) - Strategy Pattern is used to select algorithms dynamically at runtime, focusing on behavioral variability rather than instance control. Option E (Decorator Pattern) - The Decorator Pattern enhances objects with additional functionalities dynamically, unrelated to the Singleton goal of limiting instance creation.
Which city has been notified as the sole capital of Andhra Pradesh?Â
What is the development window provided under the SASCI scheme for iconic tourist destinations?Â
ADB, India sign $350 million loan to expand metro rail network in which city?
What is the key theme of the 7th Rashtriya Poshan Maah 2024, observed by the Ministry of Women and Child Development?
What is the projected cargo capacity of the Vadhvan Port by 2029?
India’s first commercial Gallium Nitride (GaN)-based Mini/Micro LED display facility approved under ISM 1.0 will be set up by:Â
Under SEBI’s new rules, what is the upper ceiling for determining materiality of Related Party Transactions (RPTs)?Â
What is the purpose of the IEPFA’s “Saksham Niveshak” campaign?Â
Which city was ranked as the world’s most polluted city in the 2024 Air Quality Index Report?
- Which chili variety from Telangana received the GI tag recently?