📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!

  • google app store apple app store
  • ✖

      Question

      Which design pattern would you use if you need to ensure

      that a class has only one instance, with a global point of access to that instance?
      A Factory Pattern Correct Answer Incorrect Answer
      B Singleton Pattern Correct Answer Incorrect Answer
      C Observer Pattern Correct Answer Incorrect Answer
      D Strategy Pattern Correct Answer Incorrect Answer
      E Decorator Pattern Correct Answer Incorrect Answer

      Solution

      The Singleton Pattern is a design pattern that restricts the instantiation of a class to one single instance and provides a global point of access to that instance. This pattern is used when exactly one instance of a class is needed to coordinate actions across the system. It ensures that there is only one instance of the class, and this instance is shared by the entire application. A common use case for the Singleton Pattern is in managing global configurations, database connections, or logging systems where multiple instances would lead to issues or unnecessary resource consumption. Singleton guarantees that a class has one and only one instance and provides a global point of access to it, ensuring controlled access to shared resources. Why Other Options Are Wrong: A) Factory Pattern: This pattern is used to create objects without specifying the exact class of object that will be created. It does not limit the number of instances. C) Observer Pattern: This pattern is used to define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified. It is unrelated to instance limitation. D) Strategy Pattern: This pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable. It focuses on algorithm variation, not instance control. E) Decorator Pattern: This pattern is used to add new functionality to an object dynamically. It does not control the number of instances of a class.

      Practice Next

      Relevant for Exams:

      ask-question