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.
Singleton pattern in action
- So now that we've talked a little bit about the singleton pattern, let's jump into our ID's and let's go create some. So back in our project, let's open up source main java and our base package and once again as we've done before we'll create a new package this one called singleton. Now we're going to create a couple classes in here. The first one we're going to create is SingA. SingA is going to be our traditional singleton so we're going to start with private static SingA instance. Then we're going to make a private constructor on this guy. Now we're going to make a public static SingA method called getInstance and you can see that IntelliJ knows what I'm doing here. It's suggesting names of methods and stuff for me. It kind of gives the feeling that this is a singleton. But we'll go ahead and continue to create ours so if null equals instance and I do prefer yoda syntax with my null checks. We will now synchronize on the class itself. This gives us a great spot to synchronize on…
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
-
-
-
-
-