singleton implementation



原文地址:http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-with-examples

Thread Safe Singleton:

           The easier way to create a thread-safe singleton class is to make the global access method synchronized,

so that only one thread can execute this method at a time. General implementation of this approach is like the below class

ThreadSafeSingleton.java

















package com.journaldev.singleton;
 
public class ThreadSafeSingleton {
 
    private static ThreadSafeSingleton instance;
     
    private ThreadSafeSingleton(){}
     
    public static synchronized ThreadSafeSingleton getInstance(){
        if(instance == null){
            instance = new ThreadSafeSingleton()                 
            return instance;
    }
      }
      Above implementation works fine and provides thread-safety but it reduces the performance
because of cost associated with the synchronized method, although we need it only for the first few
threads who might create the separate instances (Read: Java Synchronization). To avoid this extra
overhead everytime, double checked locking principle is used. In this approach, the synchronized block
is used inside the if condition with an additional check to ensure that only one instance of singleton
class is created.

Below code snippet provides the double checked locking implementation.

public static ThreadSafeSingleton getInstanceUsingDoubleLocking(){
     if (instance == null ){
         synchronized (ThreadSafeSingleton. class ) {
             if (instance == null ){
                 instance = new ThreadSafeSingleton();
             }
         }
     }
     return instance;
}

Bill Pugh Singleton Implementation:

  Prior to Java 5, java memory model had a lot of issues and above approaches used to fail in

certain scenarios where too many threads try to get the instance of the Singleton class simultaneously.

 So Bill Pugh came up with a different approach to create the Singleton class using a

 inner static helper class. The Bill Pugh Singleton implementation goes like this;

package com.journaldev.singleton;
 
public class BillPughSingleton {
 
     private BillPughSingleton(){}
     
     private static class SingletonHelper{
         private static final BillPughSingleton INSTANCE = new BillPughSingleton();
     }
     
     public static BillPughSingleton getInstance(){
         return SingletonHelper.INSTANCE;
     }
}

    Notice the private inner static class that contains the instance of the singleton class.

When the singleton class is loaded, SingletonHelper class is not loaded into memory and only when

someone calls the getInstance method, this class gets loaded and creates the Singleton class instance.

This is the most widely used approach for Singleton class as it doesn’t require synchronization.

I am using this approach in many of my projects and it’s easy to understand and implement also.

Using Reflection to destroy Singleton Pattern

      Reflection can be used to destroy all the above singleton implementation approaches.

Let’s see this with an example class.

public class EagerInitializedSingleton {
     
     private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
     
     //private constructor to avoid client applications to use constructor
     private EagerInitializedSingleton(){}
 
     public static EagerInitializedSingleton getInstance(){
         return instance;
     }
}
public class ReflectionSingletonTest {
 
     public static void main(String[] args) {
         EagerInitializedSingleton instanceOne = EagerInitializedSingleton.getInstance();
         EagerInitializedSingleton instanceTwo = null ;
         try {
             Constructor[] constructors = EagerInitializedSingleton. class .getDeclaredConstructors();
             for (Constructor constructor : constructors) {
                 //Below code will destroy the singleton pattern
                 constructor.setAccessible( true );
                 instanceTwo = (EagerInitializedSingleton) constructor.newInstance();
                 break ;
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         System.out.println(instanceOne.hashCode());
         System.out.println(instanceTwo.hashCode());
     }
}
      If your singleton class is not using a lot of resources, this is the approach to use.
But in most of the scenarios, Singleton classes are created for resources such as File System,
Database connections etc and we should avoid the instantiation until unless client calls the
getInstance method. Also this method doesn’t provide any options for exception handling
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值