Thursday, 6 November 2014

Singleton Design Pattern in java

Singleton means one instance throughout the application(Java Virtual Machine).It is used to provide global point of access to the object. In terms of practical use Singleton patterns are used in logging, caches, configuration settings etc.It is mostly used in conjunction with the Factory Design Pattern.

The below example shows a very basic implementation of singleton class 

 public class Singleton {  
      private static Singleton singletonInstance;  
      private Singleton() {  
      }  
      public static Singleton getSingletonInstance() {  
           if (null == singletonInstance) {  
                singletonInstance = new Singleton();  
           }  
           return singletonInstance;  
      }  
 }  

However this does not take care of thread safety . A better implementation will be like as follows 

 public class Singleton {  
      private static Singleton singletonInstance;  
      private Singleton() {  
      }  
      public static synchronized Singleton getSingletonInstance() {  
           if (null == singletonInstance) {  
                singletonInstance = new Singleton();  
           }  
           return singletonInstance;  
      }  
 }  

A more better implementation can be as follows -


 public class Singleton {  
 private static Singleton singletonInstance;  
 private static Object mutex = new Object();  
 private Singleton() {  
 }  
 public static Singleton getSingletonInstance() {  
 if (null == singletonInstance) {  
 synchronized(mutex){  
 singletonInstance = new Singleton();  
 }  
 }  
 return singletonInstance;  
 }  
 }  

The second example for singleton implementation guarantees thread safety but will result in poor performance as method level thread lock is acquired . The third will do synchronization only if we need to create a new instance of the singletonInstance object. This will guarantee a best performance from the above three examples. 

Wednesday, 5 November 2014

Dependency Injection


This concept says that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (the IOC container) is then responsible for hooking it all up.


Types of IoC are:
  • Constructor-based dependency injection: Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.
  • Setter-based dependency injection: Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
Example -


Setter-based dependency injection - 
 <beans>  
      <bean id="test" class="com.test">  
        <property ref="myObj"/>  
      </bean>  
      <bean id="myObj" class="com.test.MyObject">         
      </bean>       
 </beans>  



Constructor-based dependency injection - 
 <beans>  
      <bean id="test" class="com.test.Test">  
        <constructor-arg ref="myObj"/>  
      </bean>  
      <bean id="myObj" class="com.test.MyObject">         
      </bean>       
 </beans>