In this video, explore how to create singleton collections by using Java's Collections class. Learn how to create collections that can only contain one object.
- [Instructor] There are several examples…of the singleton pattern from the Java API itself,…and a similar effect is used with Java's Collections class.…The Collections class itself is not an example…of the singleton pattern,…but the class comes with three methods…that can be used to create singleton collections.…These are singletonList, singletonMap, and singletonSet.…These methods return an immutable collection.…SingletonList returns a list, singletonMap returns a map,…and singletonSet returns a set.…
They all return collections that contain only one object,…and no new objects can be added to them.…Using these methods does not turn the collection…into a singleton, because it does not stop you…creating new collections, but like a singleton,…it does provide a single point of access…that always returns the same instance.…In this example, there is a class called PrintSpooler…that manages access to printers.…In this example, it is empty, but in a real application,…it would contain codes to access a printer.…
There is also a class called NetworkDriveConnector.…
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: Java's Collection class