单例模式

package com.hanchao.test20150315;
/***********************
 * 单例模式【懒加载】
 * @author:han    
 * @version:1.0        
 * @created:2015-3-15    
 ***********************
 */
public class Singleton {

	/**
	 * 1.单例模式:确保一个类只有一个实例,并提供一个全局访问点。
	 * 条件:
	 * 《1.》私有化构造方法。
	 * 《2.》提供公开的、静态的创建方法。
	 * 《3.》使用私有的、静态的对象属相。
	 */
	
	private Singleton() {}
	
	private static Singleton singleton;
	
	/**
	 * 不考虑多线程的单例模式【懒加载】
	 * *******************
	 * @return
	 * *******************
	 * @author:wind
	 * 2015-3-15 上午11:11:05
	 * *******************
	 */
	public static Singleton getInstances() {
		if (singleton == null) {
			singleton = new Singleton();//懒加载;表示用到时才去实例化
		}
		return singleton;
	}
	
	/**
	 * 多线程下的单例模式【懒加载】
	 * 
	 * ★【synchronized关键字非常影响性能!】
	 * *******************
	 * @return
	 * *******************
	 * @author:wind
	 * 2015-3-15 上午11:10:47
	 * *******************
	 */
	public static synchronized Singleton getInstance() {
		if (singleton == null) {
			singleton = new Singleton();
		}
		return singleton;
	}
	
}

说明:这种加了synchronized的单例,比下面的单例模式,在速度上会慢一个数量级的。

package com.hanchao.test20150315;
/***********************
 * 单例模式【非懒加载】
 * @author:han    
 * @version:1.0        
 * @created:2015-3-15    
 ***********************
 */
public class Singleton2 {

	private Singleton2() {
		System.out.println("Singleton is create ");
		//创建单例的过程可能会比较慢
	}
	
	private static Singleton2 singleton = new Singleton2();
	
	/**
	 * 非延迟加载的单例模式
	 * 【无需考虑线程同步问题】
	 * *******************
	 * @return
	 * *******************
	 * @author:wind
	 * 2015-3-15 上午11:15:27
	 * *******************
	 */
	public static Singleton2 getInstance() {
		return singleton;
	}
	
	/**
	 * 说明:假如单例的创建过程很慢,而由于instance成员变量是static定义的,
	 * 		 因此在JVM加载单例类时,单例对象就会被建立,如果此时,这个单例类
	 * 		在系统中还扮演其他角色,那么在任何使用这个单例类的地方都会初始化
	 * 		这个单例变量,而不管是否被用到。
	 *  
	 *  	比如:单例类作为String工厂,用于创建一些字符串(该类既用于创建单例Singleton,又用于创建String对象)
	 */
	
}
package com.hanchao.test20150315;
/***********************
 * 单例模式【非懒加载】
 * @author:han    
 * @version:1.0        
 * @created:2015-3-15    
 ***********************
 */
public class Singleton3 {

	private Singleton3() {
		System.out.println("Singleton is create ");
		//创建单例的过程可能会比较慢
	}
	
	private static Singleton3 singleton = new Singleton3();
	
	/**
	 * 非延迟加载的单例模式
	 * 【无需考虑线程同步问题】
	 * *******************
	 * @return
	 * *******************
	 * @author:wind
	 * 2015-3-15 上午11:15:27
	 * *******************
	 */
	public static Singleton3 getInstance() {
		return singleton;
	}
	
	/**
	 * 模拟单例类扮演其他角色
	 * *******************
	 * 
	 * *******************
	 * @author:wind
	 * 2015-3-15 上午11:26:50
	 * *******************
	 */
	public static void createString() {
		System.out.println("createSting in Singleton");
	}
	
}

Singleton3.createString(); → 测试一下

package com.hanchao.test20150315;
/***********************
 * 单例模式【使用内部类代替synchronized】
 * @author:han    
 * @version:1.0        
 * @created:2015-3-15    
 ***********************
 */
public class StaticSingleton {
	
	private StaticSingleton() {
		System.out.println("StaticSingleton is create");
	}
	
	private static class SingletonHolder {
		private static StaticSingleton instance = new StaticSingleton();
	}
	
	/**
	 * 使用内部类获得单例
	 * *******************
	 * @return
	 * *******************
	 * @author:wind
	 * 2015-3-15 上午11:38:06
	 * *******************
	 */
	public static StaticSingleton getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 说明:在这个实现中,单例模式使用内部类来维护单例的实例,当StaticSingleton被加载时,
	 * 		 其内部类并不会被初始化,故可以确保当StaticSingle类被载入JVM时,不会初始化单例类,
	 * 		 而当getInstance()方法被调用时,才会加载SingletonHolder,从而初始化instance。
	 *       同时,由于实例的建立是在类加载时完成,故天生对多线程友好,getInstance()方法也不
	 *       需要使用同步关键字。
	 *       
	 *       
	 */
	

}


你可能感兴趣的:(java,设计模式,单例模式,java性能优化)