In this video, discover what the Singleton pattern is. Learn when the Singleton pattern can be used to improve your code and see some examples from the Java API.
- [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 sure that there…was only one instance of that logging class.…
Released
8/29/2018- What are creational design patterns?
- Avoid complex constructors
- Implementing the Builder pattern
- Multithreading with the Singleton pattern
- Using the Prototype pattern
- Implementing a simple Factory Method pattern
- Making code more flexible with the Abstract Factory pattern
Share this video
Embed this video
Video: When to use the Singleton pattern