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.
A person travels a certain distance at 8 km/hr and reaches 1 hr 30 minutes late. If he travels at 10 km/hr, he reaches 1 hr 30 minutes earlier. The dist...
P and Q each had designated spots to set up their temporary shops. The distance from their house (they live in the same house) to...
A truck travels at a speed of 60 km/h from city P to city Q and returns to city P by the same route at a speed of 100 km/h. What is the average speed of...
Aman has to travel a distance of 240 km. He travels the first 25% of the distance at a speed of 50 kmph, the next two-fifths at a speed of 48 kmph, and ...
A Scooty is currently traveling at a speed of (2a – 90) km/hr, and a Bus is moving at a speed of (b + 30) km/hr. If the Scooty's speed were equal to t...
A cyclist moves at a speed of 20 km/h and plans to complete his journey in 3 hours. After finishing half the journey, he takes a break of 30 minutes. At...
A man travels some distance at a speed of 15 km/hr and returns at a speed of 9 km/hr. If the total time taken by him is 2 hrs 40 min, the distance is
In a 200-metre race, ‘A’ beats ‘B’ by 15 seconds or 60 metres. Find the time taken by ‘B’ to finish the race.
A train covers a distance of 77 km at the speed of 35 km/h the next 63 km distance covers with a speed of 42 km/h and last 24 km with the speed of 80 km...
- The ratio of the speeds of Karan and Arjun is 7:5 respectively. Manoj covers a distance of 840 km in 14 hours. If Arjun’s speed is 20% less than Manoj’...