Singleton Pattern Flavors: Check out some Singleton Design Pattern flavors.
1: Simplest: Early Initialization
public class SimpleSingleton {
private static final SimpleSingleton instance = new SimpleSingleton();
private SimpleSingleton() {
}
public static SimpleSingleton getInstance() {
return instance;
}
}
2: Lazy Initialization with improved performance but does not guarantee for single object per JVM.
Reason: Think about a multithreaded environment where two threads try to call getInstance method first time. Thread one check that instace is equal to null and before creating an object in next line it suspends and second thread get a chance to run this method. Second thread comes and see that instance is null and creates an object and returns object. Now first suspended thread starts again and since it has already checked that instance was null it again create a new object and returns. So it does not guarantee for one object per jvm.
public class SimpleSingleton {
private static SimpleSingleton instance = null;
private SimpleSingleton() {
}
public static SimpleSingleton getInstance() {
if (instance == null) {
instance = new SimpleSingleton();
}
return instance;
}
}
3: Lazy Initialization at cost of poor performance:
public class SimpleSingleton {
private static SimpleSingleton instance = null;
private SimpleSingleton() {
}
public static synchronized SimpleSingleton getInstance() {
if (instance == null) {
instance = new SimpleSingleton();
}
return instance;
}
}
4: Singleton with improved performence and guarantees single object per JVM with double checking mechanism.
public class SimpleSingleton {
private static SimpleSingleton instance = null;
private SimpleSingleton() {
}
public SimpleSingleton getInstance() {
if (instance == null) {
synchronized (SimpleSingleton.class) {
if (instance == null) {
instance = new SimpleSingleton();
}
}
}
return instance;
}
}
This blog contains my understanding and research examples in Java language. All information is only for information/discussion purpose.
Showing posts with label Singleton. Show all posts
Showing posts with label Singleton. Show all posts
Thursday, November 4, 2010
Wednesday, November 3, 2010
Factory Pattern Java Code
Factory Pattern: You can learn definition of Factory Pattern from various sources like books, java websites etc. Please look the code below for a working Dyanmic configurable factory java code.
How to use: Make a factory.properties file and put your key and class names as key value pair in this file and you are ready to use this factory.
package org.paandav.factory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class DynamicFactory {
private static final String FACTORY_FILE_NAME = "factory.properties";
private static final DynamicFactory factory = new DynamicFactory();
private static Map<String, String> keyClassMap = null;
public static DynamicFactory getInstance() throws DynamicFactoryException {
if (keyClassMap == null) {
synchronized (DynamicFactory.class) {
if (keyClassMap == null) {
initialize();
}
}
}
return factory;
}
@SuppressWarnings("unchecked")
public Object getObject(String key) throws DynamicFactoryException {
String clsName = keyClassMap.get(key);
Object obj = null;
if (null == clsName || "".equals(clsName.trim())) {
throw new DynamicFactoryException("Class for key " + key
+ " not found");
}
try {
Class cls = Class.forName(clsName);
obj = cls.newInstance();
} catch (Exception cnf) {
throw new DynamicFactoryException(clsName + " can not instatitate.");
}
return obj;
}
private static void initialize() throws DynamicFactoryException {
try {
File pfile = new File(FACTORY_FILE_NAME);
Properties pro = null;
InputStream is = null;
if (pfile.exists()) {
pro = new Properties();
is = new FileInputStream(pfile);
pro.load(is);
if (pro.size() > 0) {
keyClassMap = new HashMap<String, String>();
Set<String> keys = pro.stringPropertyNames();
for (String key : keys) {
if (null != pro.getProperty(key)
&& !"".equals(pro.getProperty(key).trim())) {
keyClassMap.put(key, pro.getProperty(key));
}
}
}
}
} catch (Throwable th) {
keyClassMap = null;
throw new DynamicFactoryException(th.getMessage());
}
}
}
package org.paandav.factory;
public class DynamicFactoryException extends Exception {
private static final long serialVersionUID = 1L;
public DynamicFactoryException() {
super();
}
public DynamicFactoryException(String msg) {
super(msg);
}
}
How to use: Make a factory.properties file and put your key and class names as key value pair in this file and you are ready to use this factory.
package org.paandav.factory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class DynamicFactory {
private static final String FACTORY_FILE_NAME = "factory.properties";
private static final DynamicFactory factory = new DynamicFactory();
private static Map<String, String> keyClassMap = null;
public static DynamicFactory getInstance() throws DynamicFactoryException {
if (keyClassMap == null) {
synchronized (DynamicFactory.class) {
if (keyClassMap == null) {
initialize();
}
}
}
return factory;
}
@SuppressWarnings("unchecked")
public Object getObject(String key) throws DynamicFactoryException {
String clsName = keyClassMap.get(key);
Object obj = null;
if (null == clsName || "".equals(clsName.trim())) {
throw new DynamicFactoryException("Class for key " + key
+ " not found");
}
try {
Class cls = Class.forName(clsName);
obj = cls.newInstance();
} catch (Exception cnf) {
throw new DynamicFactoryException(clsName + " can not instatitate.");
}
return obj;
}
private static void initialize() throws DynamicFactoryException {
try {
File pfile = new File(FACTORY_FILE_NAME);
Properties pro = null;
InputStream is = null;
if (pfile.exists()) {
pro = new Properties();
is = new FileInputStream(pfile);
pro.load(is);
if (pro.size() > 0) {
keyClassMap = new HashMap<String, String>();
Set<String> keys = pro.stringPropertyNames();
for (String key : keys) {
if (null != pro.getProperty(key)
&& !"".equals(pro.getProperty(key).trim())) {
keyClassMap.put(key, pro.getProperty(key));
}
}
}
}
} catch (Throwable th) {
keyClassMap = null;
throw new DynamicFactoryException(th.getMessage());
}
}
}
package org.paandav.factory;
public class DynamicFactoryException extends Exception {
private static final long serialVersionUID = 1L;
public DynamicFactoryException() {
super();
}
public DynamicFactoryException(String msg) {
super(msg);
}
}
Subscribe to:
Posts (Atom)