`
子衿青青
  • 浏览: 105820 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

单例模式

 
阅读更多

 本文章不针对任何项目,只是在此做个小结。

 

Java有三种单例模式的实现方法:饿汉式、懒汉式和登记式。

1、饿汉式单例类:

public class TestSingleton1 {
	
	private TestSingleton1(){
	}
	
	//自行实例化
	private static  TestSingleton1 instance = new TestSingleton1();	
	
	//静态工厂方法
	public static TestSingleton1 getInstance(){
		return instance;
	}
}

 

个人感觉:饿汉式单例,使用资源多但速度快,如果用的类不是很大的话,饿汉式单例方法是比较好的。

 

2、懒汉式单例类:

public class TestSingleton2 {
	
	private TestSingleton2(){
	}
	
	//无final
	private static TestSingleton2 instance = null;
	
	//静态工厂方法,加上synchronized同步的话,比较耗内存,可以参考双重检查加锁方法,如是单线程,无需同步
	public synchronized static TestSingleton2 getInstance(){
		if (instance == null ) {
			instance = new TestSingleton2();
		}
		
		return instance;
	}
}

懒汉式单例类是对饿汉式单例类的一个性能优化。

在懒汉式单例类中,如果类没有被使用,就不会再创建类的实例了,从而减少内存的消耗。

 

个人感觉:懒汉式单例类是一种延时加载,反应时间上变长了。它要考虑多个线程首次引用单例类时的访问问题,加上synchronized同步的话,比较耗内存,为解决此种情况,就加锁,加锁以后,就只能在一个线程创建完对象后另一个线程才能使用这个类,保证了类的单一性,当然也可以使用双重检查加锁double-check。

双重检查加锁double-check:

public class TestSingleton21 {
	
	private volatile static TestSingleton21 Instance;
	
	private TestSingleton21(){
	}
	
	//双重检查加锁
	public static TestSingleton21 getInstance(){
		if (Instance == null) {
			synchronized (TestSingleton21.class) {
				if (Instance == null) {
					Instance = new TestSingleton21();
				}
			}
		}
		
		return Instance;
	}
}

 不过,double-check 也是不安全的。

 有一种比较流行的静态内部类持有外部类的实例,使用jvm的类载入机制保证单例模式在多线程环境下的正确加载:

public class TestSingleton22 {
	
	private TestSingleton22(){
	}
	
	//静态内部类持有外部类的实例
	public static TestSingleton22 getInstance(){
		return TestInstance.getInstance();
	}
	
	//私有类
	private static class TestInstance{
		private static TestSingleton22 instance = new TestSingleton22();
		
		private TestInstance(){
		}
		
		private static TestSingleton22 getInstance(){
			return instance;
		}
	}
}

 

只能说,这种单例模式现在比较流行,但不是什么情况下使用它都最适合,还要看具体情况。

3、登记式单例类:

public class TestSingleton3 {

	private static Map<String, TestSingleton3> map = new HashMap<String, TestSingleton3>();

	static {
		TestSingleton3 instance = new TestSingleton3();
		map.put(instance.getClass().getName(), instance);//注册类名
	}

	// 保护默认的构造器
	protected TestSingleton3() {
	}

	public static TestSingleton3 getInstance(String name) {
		if (name == null) {
			name = TestSingleton3.class.getName();
			System.out.println("name: " + name);
		}
		if (map.get(name) == null) {
			try {
				map.put(name, (TestSingleton3) Class.forName(name)
						.newInstance());
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

		return map.get(name);
	}

	private String say() {
		return "演讲开始。。。";
	}

	public static void main(String[] args) {
		TestSingleton3 instance = TestSingleton3.getInstance(null);
		System.out.println(instance.say());
	}

}

此种方法是对一组单例类的管理和维护,实例全部被放进map中,对登记过的直接返回,对没登记过的登记注册再返回。是一种整体思路。

 

由于不针对项目,在此只做个小结,有想法或者问题的或者文章有错误的地方,欢迎大家指正。谢谢!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics