From the course: Spring: Design Patterns
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
The Singleton pattern
- The singleton pattern is the one pattern that I use almost as much as the factory pattern and with Spring I'm using that a lot more than I usually had because everything in Spring is a singleton. Well almost everything. So in Spring itself, every bean by default is a singleton. Unless you tell the application context otherwise, you're going to get a singleton. Now one thing to note is that this is not a classic singleton which is what we're going to discuss, but it behaves the exact same during the run time of your application. Beans, therefore, because they are singletons must be designed in a way that is thread safe. And that applies to all singleton classes in general. So this pattern is relatively straightforward. You have a class that stores a reference to itself. More specifically, to an instance of itself. You have a private constructor. Then you have a static getInstance method that returns a reference of self or if self is nul, it constructs it using the private constructor…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
The Factory pattern4m 39s
-
(Locked)
Factory pattern in action5m 38s
-
(Locked)
The Builder pattern2m 55s
-
(Locked)
Builder pattern in action6m 1s
-
(Locked)
The Singleton pattern4m 1s
-
(Locked)
Singleton pattern in action4m 55s
-
(Locked)
The Prototype pattern4m 36s
-
(Locked)
Prototype pattern in action4m 59s
-
-
-
-
-