October 2017
Intermediate to advanced
396 pages
10h 2m
English
In the following code example, I will be creating a class with a method to create an instance of this class if one does not exist. If the instance is already present, then it will simply return the reference of that object. I have also taken thread safety into consideration, and so I have used a synchronized block here before creating the object of that class.
Let's check out the UML diagram for the Singleton design pattern:
package com.packt.patterninspring.chapter2.singleton.pattern; public class SingletonClass { private static SingletonClass instance = null; private SingletonClass() { } public static SingletonClass getInstance() { if (instance == null) { synchronized(SingletonClass.class){ ...Read now
Unlock full access