Friday, October 3, 2008

Evolution of Singleton Pattern

Singleton is one of those design patterns which is encountered by almost every software developer irrespective of their background. This fact can be attributed to the usefulness of the pattern itself.
Though critics have singled out singleton as an anti pattern, who feel that it is overly used.

Apart from design considerations, what is even more important is the way Singleton pattern is implemented. In this post i will walk through the implementation of a singleton starting from the naive approach to the fully functional thread safe implementation and in the last the most elegant solution using Java 5.0 additions to the language.


Naive implementation: Traditional way requires to declare constructor's access specifier to be private/protected and using static factory method to retrieve the instance of class as shown below.

 public class Singleton {
private final static Singleton INSTANCE = null;

protected Singleton() {}



public static Singleton getInstance() {
if(INSTANCE == null)
INSTANCE = new Singleton();
return
INSTANCE
}
}
Disadvantages:
  • This solution in not thread safe and in case of concurrent calls to
    getInstance()can result in multiple instances of Singleton.
Thread Safe Implementation(s):

a)
 public class Singleton {
public final static Singleton INSTANCE = new Singleton();
   protected Singleton() {}

public static Singleton getInstance() {
return INSTANCE;
}

}

Disadvantage:
  • This implementation is particularly not suitable where lazy initialization is sought, for eg. where Singleton is used to encapsulate objects having large memory footprints.
b)
 public class Singleton {
private static volatile Singleton INSTANCE;

// Protected constructor is sufficient to suppress unauthorized calls to the constructor
protected Singleton() {}

public static synchronized Singleton getInstance() {
if (INSTANCE == null)
INSTANCE = new Singleton();
return INSTANCE;
}
}

Disadvantage:
  • The above solution though provides lazy initialization but have a performance drawback as every call to getInstance() method has to acquire class lock on Singleton to proceed.
Following implementations c) and d) address this performance drawback and also provides thread safe/lazy initialized behavior.



c)
Double check locking:

 public class Singleton {
private static volatile Singleton INSTANCE;

// Protected constructor is sufficient to suppress unauthorized calls to the constructor
protected Singleton() {}

public static Singleton getInstance() {
if (INSTANCE == null)
synchronized(Singleton.class) {
if (INSTANCE == null)
INSTANCE = new Singleton();
}
}
return INSTANCE;
}
}


d)
This implementation rely on a static inner class SingletonHolder
which provides the lazy behavior.
 public class Singleton {

protected Singleton() {}

/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.instance , not before.
*/

private static class SingletonHolder {
private final static Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}

I personally favor implementation the above solution d) just for sheer elegance and simplicity.
Now as if it looks we have got the best Singleton implementation at place,
here is the caveat, all the solutions that we have encountered so far are vulnerable to serialization and reflection attacks.
Serialization problem:- As implicit Deserialization behavior of JVM rely on the call to default constructor of the object, checks are required in case the Singleton object is serializable. This can be done by overloading readResolve() method of the api.

Reflection attack:- Not many people know that, in a code trusted environment it is possible to invoke private constructors by using AccessibleObject api. Thus leading to more than one instance of Singleton.


There is one more solution which takes care of the above mentioned attacks and is also thread safe. Only drawback is that it runs on jvm's supporting latest versions of java starting from java5.
It relies on
a single-element enum type paradigm as shown below:

public enum Singleton {
INSTANCE;
}

With this we come to the end of Singleton journey for now. Look out for the posts as I try to dissect some other design pattern.-:)




Monday, September 8, 2008

Enterprise Mashups - time to relook and mature the concept.

Enterprise Mashups the concept has been around for couple of years now. Till now it has been a hot property in the developer world. In this post I will make an attempt to identify the various stakeholders involved in the development of mash ups and also provide key success criteria for a mashup platform.

Mashup Stake holders and platform requirements:
Some people also describe mashups to be situational applications which according to me in the enterprise context does not do justice to the concept. Situational applications are applicable for web end users who looks to play around with their favorites (provided as services) over the web. This assumption of situational application have so far directed the thought process behind the mashups. It has stick to the realms of an enthusiastic developer's presentation of a hypothetical business context where in 80% of times the mashup culminated in a location based service ending up displaying Google/yahoo maps. Though this visually invigorating appeal was required for the concept to get approval among various stakeholders but now the time has come to mature it further. So who can be the majority of the mashup users in an enterprise?
They without any doubt would be business analysts or domain experts. Thus any mashup platform should cater to the basic requirements of these domain experts for being successful.
Now lets have a look on the list of basic functionality of a mashup platform.
  • Service discovery and categorization:- As mashup is built upon service and is also a service, platform shall be capable of discovering the services along with the support of categorizing them.
  • Service publishing
  • Security :- The platform shall provide secure access to services depending upon the users. It should have inbuilt support to integrate with corporate databases and directories.
  • Analysis tools integration:-A process map created using a tool like Visio can be directly used as a enabler point to drag and build service mashup.
  • Service invocation and preview: The platform shall have provide the capability to invoke the services and view the results using different previews tables, charts, tree hierarchy etc.
With various frameworks competing for the flesh it would be interesting to see who identify the potential users first and adapt their platform accordingly.

Sunday, September 7, 2008

Introduction

Its been around 4 years that Java and I have been together. In these years there have been countless instances where content published by blogger's have made me understand java and related technologies better. This same feeling of learning and sharing prompted me to start with my own blog on tryst with java. I am very optimistic that with time this blog will be as useful to other developers as many others have been to me. I will appreciate your comments as I go along with the postings.