package com.doctor.java.design_pattern;import org.slf4j.logger;import org.slf4j.loggerfactory;/ Several implementations of ** * Singleton mode--"Java Development technology-experience the beauty of design patterns and algorithms in architecture" * * @author doctor * * @time 2015 April 24 PM 11:11:03 */public class singletonpattern {/** * @param args */public static void main (String[] args) { Lazysingleton.getinstance (); Lazysingleton.getinstance (); Lazysingleton.getinstance ();//eagersingleton.getinstance (); Eagersingleton.getinstance ();//doublechecklazysingleton.getinstance ();D oublechecklazysingleton.getinstance ();D Oublechecklazysingleton.getinstance ();//singleton.getinstance (); Singleton.getinstance ();//enumsingleton.instance.enumsingletonoperation (); EnumSingleton.instance.EnumSingletonOperation ();} /** * Lazy Single-case, thread-safe */public static class lazysingleton {private static final logger log = loggerfactorY.getlogger (lazysingleton.class);// private static object, not initialized at load time Private static lazysingleton singleton = null;// private construction method, avoid external creation instance Private lazysingleton () {log.info ( LazySingleton.class.getName () + "Singleton constructor call");} /** * static Factory method, returns a unique instance of this class initializes * * @return */public when the instance is not initialized Static synchronized lazysingleton getinstance () {if (singleton == null) {singleton = new lazysingleton ();} Return singleton;}} /** * Lazy single case, thread safety, double lock increase access speed */public static class DoubleCheckLazySingleton {Private static final logger log = loggerfactory.getlogger ( Doublechecklazysingleton.class);// private static object, not initialized at load time Private static doublechecklazysingleton singleton = null;// private construction methods to avoid creating instances externally Private doublechecklazysingleton () {log.info (DoubleCheckLazySingleton.class.getNAme () + "Singleton constructor call");} /** * static Factory method, returns a unique instance of this class initializes * * @return */public when the instance is not initialized Static doublechecklazysingleton getinstance () {if (singleton == null) { synchronized (Doublechecklazysingleton.class) {if (singleton == null) {singleton = new doublechecklazysingleton ();}}} Return singleton;}} /** * a hungry man type single case */private static class eagersingleton {private static final logger log = loggerfactory.getlogger (eagersingleton.class);p rivate static final eagersingleton instance = new eagersingleton ();p rivate Eagersingleton () {log.info (EagerSingleton.class.getName () + "Singleton constructor call");} Public static eagersingleton getinstance () {return instance;}} /** * Lazy Load + thread safety = internal class + static initialization multithreading default sync lock */public static class Singleton {private static final Logger log = Loggerfactory.getlogger (Singleton.class);p Rivate singleton () {log.info (Singleton.class.getName () + "Singleton constructor call");} Public static singleton getinstance () {return singletonholder.instance;} The inner class of the/** * class, which is a static member inner class, has an instance of the inner class that has no binding relationship to an instance of an external class, and is only loaded when called to, and deferred loading from implementation load */private static class singletonholder {// static initialization, which is guaranteed by the JVM for thread safety private static singleton Instance = new singleton ();}} The/** * enumeration implements single-instance control, provides serialization, and is secured by the JVM */public static enum enumsingleton {// defines an enumeration element that represents only one instance of Enumsingleton. instance;/** * instance method, a singleton can have its own operation */public void enumsingletonoperation () {// function processing}}}
Several implementations of the singleton pattern-Java Development Technology-experience the beauty of design patterns and algorithms in architecture