zoukankan      html  css  js  c++  java
  • JAVA 之 七种单例模式

    1、饿汉式单例类

    class EagerSingleton {   
        private static final EagerSingleton instance = new EagerSingleton();   
        private EagerSingleton() { }   
      
        public static EagerSingleton getInstance() {  
            return instance;   
        }     
    }  
    

    2、懒汉式单例类

    class LazySingleton {   
        private static LazySingleton instance = null;   
      
        private LazySingleton() { }   
      
        synchronized public static LazySingleton getInstance() {   
            if (instance == null) {  
                instance = new LazySingleton();   
            }  
            return instance;   
        }  
    }  
    

      或者

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

    3、双重检查锁定-懒汉式单例

    class LazySingleton {   
        private volatile static LazySingleton instance = null;   
      
        private LazySingleton() { }   
      
        public static LazySingleton getInstance() {   
            //第一重判断  
            if (instance == null) {  
                //锁定代码块  
                synchronized (LazySingleton.class) {  
                    //第二重判断  
                    if (instance == null) {  
                        instance = new LazySingleton(); //创建单例实例  
                    }  
                }  
            }  
            return instance;   
        }  
    }  
    

    4、Initialization Demand Holder (IoDH)静态内部类之单例模式

    class Singleton {  
        private Singleton() {  
        }  
          
        private static class HolderClass {  
                private final static Singleton instance = new Singleton();  
        }  
          
        public static Singleton getInstance() {  
            return HolderClass.instance;  
        }  
         
    }
    

    IoDH可以实现延迟加载,又可以保证线程安全,不影响系统性能,不失为一种最好的Java语言单例模式实现方式(其缺点是与编程语言本身的特性相关,很多面向对象语言不支持IoDH)。

    5、序列化和反序列化 -- 单例模式

    public class SingletonTwo implements Serializable{
    
    	/**
    	 * 序列化与反序列化方式
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	private static class SingletonHan{
    		private static final SingletonTwo single = new SingletonTwo();
    	}
    	
    	private SingletonTwo(){}
    	
    	public static SingletonTwo getInstance(){
    		return SingletonHan.single;
    	}
    	
    	/**
    	 * 反序列化接口
    	 * @return
    	 * @throws ObjectStreamException
    	 */
    	protected Object readResolve() throws ObjectStreamException{
    		return SingletonHan.single; 
    	}
    }
    

      

    6、使用static代码块 

    public class Singleton {
    
    	private static Singleton instance = null;
    	
    	private Singleton(){}
    	/**
    	 * 静态代码块实现
    	 */
    	static{
    		instance = new Singleton();
    	} 
    	
    	public static Singleton getInstance(){
    		return instance;
    	}
    }
    

      

    7、使用enum枚举数据类型

    /**
     * enum枚举实现单例模式
     * @author MIXCS
     *
     */
    public class SingletonEnum {
    
    	public enum EnumSingle{
    		singleton;
    		private SingletonEnum instance;
    		
    		private EnumSingle(){
    			instance = new SingletonEnum();
    		}
    		
    		public SingletonEnum getSingele(){
    			return instance;
    		}
    	}
    	
    	public static SingletonEnum getInstance(){
    		return EnumSingle.singleton.getSingele();
    	}
    }
    

      

  • 相关阅读:
    C# send mail with outlook and word mailmerge
    The ‘Microsoft.ACE.OLEDB.12.0′ provider is not registered on the local machine. (System.Data)
    显示数据库中所有表的记录数
    Transaction Log Truncation
    To change the sharepoint CA port
    sharepoint One-Time Passwords (windows basic authentication)
    Multi-Device Hybrid Apps (Preview)
    0ffice365 Calendar API
    angular service/directive
    MySql安装图解
  • 原文地址:https://www.cnblogs.com/binbang/p/6402238.html
Copyright © 2011-2022 走看看