From the course: Java EE: Design Patterns and Architecture

Unlock the full course today

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

The Singleton design pattern

The Singleton design pattern

From the course: Java EE: Design Patterns and Architecture

Start my 1-month free trial

The Singleton design pattern

- [Instructor] What is the motivation for the singleton pattern? Well it's about having a single instance of an object in the JVM that is only ever instantiated once within the life cycle of the application. It provides a global point of access to the object, which once created is not destroyed until the application terminates. But why do you need this single object in the first place? The reason is that heavy weight objects, such as connection pools and loggers, are expensive to construct, and therefore you would not want to create them every time you require a connection, or you want to output a message to a log. These objects should be created only once by the singleton, and they're made available to the entire application for the life of the application. If you ever implemented the singleton pattern in Java SE, or in other language, you will know that it's implementation is really quite non-trivial. You have to start with some pretty unnatural constructs, like double-check…

Contents