Java Singleton Design Pattern
Java Singleton Design Pattern is the most commonly used pattern in the object oriented world.
This design pattern insure that only on instance of a class is created by the JVM.
Implementing The Singleton Pattern (Java)
Singletons are used across all the object oriented supported languages such as C#, Java, C+ ,etc..
The Singleton Class should implement the following behaviour :
- Thread Safe
- One instance of a class is created
- Global point of access
- No effect on any client objects
- Lazy instantiation
Thread Safe Java Singleton
Here you see a code snippet – you may use any of this code freely as long as you reference
Everything is explained in the comments.
/* MonkeyTool
* Copyright (C) 2012, sshadmincontrol.com
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
*/
public class SessionManager {
// LOCK
private static final Object LOCK = new Object();
//garbage collected safe live reference
private static SessionManager INSTANCE;
/**
* Provide a default Private constructor
* constructor is private
*/
private SessionManager() {
super();
}
/**
* @return the instance
*/
public static SessionManager getInstance() {
// Synchronize on LOCK to ensure that we don't end up creating
// two singletons.
synchronized (LOCK) {
if (null == INSTANCE) {
//lazy initialization
SessionManager controller = new SessionManager();
INSTANCE = controller;
}
}
return INSTANCE;
}
}
|
Double Checked Locking Java Singleton
The second check at makes it impossible for two different Singleton
objects to be created. The following code works fine for multithreaded access to the getInstance() method.
Here you see a code snippet – you may use any of this code freely as long as you reference
http://sshadmincontrol.com somewhere in your code.
Everything is explained in the comments.
/* MonkeyTool
* Copyright (C) 2012, sshadmincontrol.com
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
*/
public class SessionManager {
//garbage collected safe live reference
private static SessionManager INSTANCE;
/**
* Provide a default Private constructor
* constructor is private
*/
private SessionManager() {
super();
}
/**
* @return the instance
*/
public static SessionManager getInstance() {
if (null == INSTANCE) {
//double-checked locking
synchronized(SessionManager.class){
if (null == INSTANCE) {
INSTANCE = new SessionManager();
}
}
}
return INSTANCE;
}
}
|
Singleton implementation with static field
The best solution is to use a Singleton implementation with static field. This is a good alternative if your objective is to eliminate synchronization. A simple way to create a Singleton.
Here you see a code snippet – you may use any of this code freely as long as you reference
http://sshadmincontrol.com somewhere in your code.
Everything is explained in the comments.
/* MonkeyTool
* Copyright (C) 2012, sshadmincontrol.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
*/
public class SessionManager {
//final cannot be extended
private final static SessionManager INSTANCE = new SessionManager();
/**
* Provide a default Private constructor
* constructor is private
*/
private SessionManager() {
super();
}
/**
* @return the instance
*/
public static SessionManager getInstance() {
return INSTANCE == null ? new SessionManager() : INSTANCE;
}
}
|
0 comments:
Post a Comment