Design Mode - Singleton pattern JAVA implemented

Singleton pattern is actually a development model for the underlying business environment is not in use to many. Here for the time being to obtain information warehouse to make an example of when the project started.

First, the starving mode, when the project will be a start of the object required to instantiate it. Although, you may not necessarily take up memory will be used, but in fact the possibility of cases will not be used to write a minimum, but do not rule out some business classes may change due to the migration of business to be destroyed, so It seems there is some truth.

Singleton starving type is thread-safe, because in a multi-threaded before initiating this object has existed
package Singleton;

/**

  • Singleton, starving formula
  • @author [email protected]
  • */
    public class WmsSingleton {

    // create only here once, are thread-safe
    private static WmsSingleton singleton = new WmsSingleton ( );

    private WmsSingleton() {}

    public static WmsSingleton getInstance() {

    System.out.println ( "object before acquiring object information:" + Singleton);
    return Singleton;
    }
    }

General lazy are not necessarily thread-safe, so-called thread-safe, simple terms if you want to have to modify the action of the static constant class, it may be related to a non-thread-safe

package Singleton;

/**

  • Singleton, lazy formula
  • @author [email protected]
  • */
    public class WmsLazySingleton {

    private static WmsLazySingleton singleton;

    private WmsLazySingleton() {}

    public static WmsLazySingleton getInstance() {

     if (singleton == null) {
         System.out.println("获取对象前对象信息:"+singleton);         
         singleton = new WmsLazySingleton();
         System.out.println("获取对象后对象信息:"+singleton);
     }
     return singleton;

    }
    }

Since generally lazy formula may be related to thread safety issues, consider the appropriate method for the block add about synchronization lock, to solve this problem. Here synchronized locks objects must not be null

You can not imagine to lock a void, there must be an entity that can play a role lock
package Singleton;

/**

  • Singleton, formula-fed + synchronized lock to ensure thread safety
  • @author [email protected]
  • */
    public class WmsLazySyncSingleton {

    private static WmsLazySyncSingleton lazySyncSingleton = null;

    private static Object lock = new Object();

    private WmsLazySyncSingleton(){}

    public static WmsLazySyncSingleton getInstance(){

    //加同步锁
    synchronized(lock) {
    
        if (lazySyncSingleton == null) {
    
            lazySyncSingleton =  new WmsLazySyncSingleton();
        }
    }
    return lazySyncSingleton;

    }

}

Static inner classes to implement lazy man's way. This is based on JAVA time to load static inner classes to implement the
package Singleton;

/**

  • Static inner classes manner
  • @author [email protected]
  • */
    public class WmsStaticClassSingleton {

    // static inner class object is initialized
    private static class StaticClassSingleton {

     private static WmsStaticClassSingleton staticClassSingleton = new WmsStaticClassSingleton();

    }

    private WmsStaticClassSingleton(){}

    public static WmsStaticClassSingleton getInstance(){
    return StaticClassSingleton.staticClassSingleton;
    }
    }

Guess you like

Origin blog.51cto.com/4890631/2485497