From the course: Java Design Patterns: Creational

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

When to use the Singleton pattern

When to use the Singleton pattern - Java Tutorial

From the course: Java Design Patterns: Creational

Start my 1-month free trial

When to use the Singleton pattern

- [Instructor] The Singleton pattern should be used when you want to make sure that only one instance of a class can be created. There might be times when you want to make it impossible to create multiple instances of a class while still making sure that there is a way to access that one instance. So in what situation would you need to do this? Some typical examples include window managers, database connectors, file managers, user interface dialogs, and resource accessors and spoolers. Say, for example, that you have a class that controls access to a resource like a printer. You might have multiple printers, but you would want to make absolutely sure that only one instance of the class that controls access to them could be created. If there are multiple print spoolers, they might try and allow two different processes access to the printer at the same time. Or say you had a logging class that needs to be used repeatedly by all the files in your project. Again, you would want to make…

Contents