Which design pattern guarantees that only one instance of a class exists?

Prepare for the TJR Bootcamp Test with quizzes and flashcards. Each question includes hints and explanations to boost your readiness for the exam!

Multiple Choice

Which design pattern guarantees that only one instance of a class exists?

The idea being tested is ensuring that only one instance of a class exists throughout the program, with a single, global access point to that instance. This is achieved by the Singleton pattern. In a singleton, the class keeps a private static reference to its sole instance and prevents external code from creating new objects by making the constructor private. When the instance is first requested, the class creates it and stores it; later requests return the same stored instance, so every part of the program uses the exact same object. In multithreaded contexts, you typically add synchronization or lazy initialization to ensure only one instance is created even if several threads try to access it at once. This pattern is handy for things like a central configuration manager or a logging service where having multiple copies could cause inconsistent state.

Other patterns don’t guarantee a single instance: a Factory focuses on creating objects and can produce many instances; an Observer is about notifying listeners of changes; an Adapter changes one interface into another.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy