Singleton Design Pattern
- 13
- June
- 2009
If we are talking about Design Patterns, One of the most common pattern that crosses your mind is Singleton Design Pattern.
The underlying definition of the same is “Ensure a class only has one instance, and provide a global point of access to it.”
Even though it is simple and most of you might already know this, but at times it gets confusing, And as there is No right or wrong approach to it, I will discuss a few of the most common approaches in this post.
Approach 1
In this approach we create a static final variable which will hold our unique instance.
Next we make sure that no can access the constructor to create a new instance, So we make the constructor private.
Now in order to allow access to this instance we create a public Static method which returns you the uniquely created instance of our class.
public class Nitin {
private static final Nitin singletonInstance = new Nitin(); // my unique instance
// Private constructor prevents instantiation from other classes
private Nitin() {
}
public static Nitin getInstance() {
return singletonInstance;
// returns you the same instance we created
}
}
}
Approach 2 – using a static block of code to instantiate.
private static Nitin singletonInstance = null; // instance has not been
// created yet.
// Private constructor prevents instantiation from other classes
private Nitin() {
}
static {
singletonInstance = new Nitin();
// ensures that the instance is only created when it is first needed.
}
}
Approach 3 – we can check in the getInstance method if the instance was created. It ensures that your instance is only created when it is needed.
public class Nitin {
private static Nitin singletonInstance = null; // instance not created yet
// Private constructor prevents instantiation from other classes
private Nitin() {
}
public static Nitin getInstance() {
if (singletonInstance == null) {
singletonInstance = new Nitin(); // my unique instance
}
return singletonInstance;
}
}
Well as we say that there is always a way out 🙂 If you want to break something then no one can stop you…
There is a crack to singleton implementation which I came across recently. Even though you should not try that on someone’s code but there is no harm if you just did for fun,
class CrackingSingleton {
public static void main(String[] args) throws IllegalArgumentException,
InstantiationException, IllegalAccessException,
InvocationTargetException, ClassNotFoundException {
Class clazz = Class.forName("Nitin"); // loads the class
Constructor[] cons = clazz.getDeclaredConstructors();
// gets the constructors
cons[0].setAccessible(true); // changes the access rights
Nitin noMoreSingleton = (Nitin) cons[0].newInstance();
// we have our new instance
}
}
[ad]
Related
3 Responses so far.
Recent Posts
- 12.1.0.2 RAC Installation – Step by Step installation on VMware
- OSB FTP Poller continuously throwing exception – while no file pending on FTP Location
- Using JMS Transport with OSB
- Installing Spring Tool Suite and configuring Spring for a development PC
- Rest Enabling SOA using OSB in Oracle 12c
Recent Comments
- Nits on Installing Oracle SOA Suite 12c
- Rajesh Krishna on Installing Oracle SOA Suite 12c
- Nits on Installing Oracle SOA Suite 12c
- Rohinii on Installing Oracle SOA Suite 12c
- Mahitha on Oracle Service Bus (OSB) Development Best Practices.
Archives
- December 2015
- May 2015
- November 2014
- September 2014
- August 2014
- July 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- May 2013
- March 2013
- February 2013
- November 2012
- October 2012
- September 2012
- May 2012
- March 2012
- February 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- June 2011
- May 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- March 2010
- February 2010
- December 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- August 2008
- June 2008
- May 2008
Categories
- AIA
- AXIS2
- Best Practices
- BPEL
- BPM
- Budget
- Build
- Design Patterns
- ESB
- Hermes
- Hibernate
- Home Owners
- Java
- JMS
- JSR 168
- Maven
- Mediator
- Messaging
- Nitin
- OER
- OESB
- Oracle
- OSB
- OWSM
- Portlets
- SDLC
- Sequencing
- Singleton
- SOA
- SOAP
- Spring
- Struts
- UK
- Uncategorized
- Web Services
- WLST
Meta
To find out more, including how to control cookies, see here: Cookie Policy
Recent Posts
- 12.1.0.2 RAC Installation – Step by Step installation on VMware
- OSB FTP Poller continuously throwing exception – while no file pending on FTP Location
- Using JMS Transport with OSB
- Installing Spring Tool Suite and configuring Spring for a development PC
- Rest Enabling SOA using OSB in Oracle 12c
Recent Comments
- Nits on Installing Oracle SOA Suite 12c
- Rajesh Krishna on Installing Oracle SOA Suite 12c
- Nits on Installing Oracle SOA Suite 12c
- Rohinii on Installing Oracle SOA Suite 12c
- Mahitha on Oracle Service Bus (OSB) Development Best Practices.
Archives
- December 2015
- May 2015
- November 2014
- September 2014
- August 2014
- July 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- May 2013
- March 2013
- February 2013
- November 2012
- October 2012
- September 2012
- May 2012
- March 2012
- February 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- June 2011
- May 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- March 2010
- February 2010
- December 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- August 2008
- June 2008
- May 2008
Categories
- AIA
- AXIS2
- Best Practices
- BPEL
- BPM
- Budget
- Build
- Design Patterns
- ESB
- Hermes
- Hibernate
- Home Owners
- Java
- JMS
- JSR 168
- Maven
- Mediator
- Messaging
- Nitin
- OER
- OESB
- Oracle
- OSB
- OWSM
- Portlets
- SDLC
- Sequencing
- Singleton
- SOA
- SOAP
- Spring
- Struts
- UK
- Uncategorized
- Web Services
- WLST
Singletons will also not work if Singleton class extends a class & that super class implements clonnable interface & overrides clone method.
Your Singleton implementation was not thread safe :), but anyways your “CrackingSingleton” will work fine unless we properly set the JVM properties.
Also, Singletons have problem in clustered environment as well 🙂
@ThreadSafe
public static Nitin getInstance() {
if (INSTANCE!= null) return INSTANCE; // we are optimistic
else{
synchronized(this){ //we do not want two thread to enter this area
if (INSTANCE== null) { // check for the second thread which enters.
INSTANCE= new Nitin(); // my unique instance
}
}
return INSTANCE;
}
}
}