如果要保证系统里一个类最多只能存一个实例时,就需要单例模式
1)最简单的单例
[img]http://dl.iteye.com/upload/attachment/486570/1ebae9e0-634a-3230-8c85-ceed3ac9ba16.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/486575/6303bf8e-2c27-37e6-a743-d1c416950aab.jpg[/img]
2)延迟创建
延迟实例化单例对象(static属性在加载类时就会被初始化),即在第一次使用该类的实例时才实例化
public class UnThreadSafeSingleton{
private static UnThreadSafeSingleton = null;
public static UnThreadSaceSingleton getInstance(){
if(instance==null){
instance = new UnThreadSafeSingleton();
}
return instance;
}
}
此方法不是线程安全的
[img]http://dl.iteye.com/upload/attachment/486584/6cce968c-1264-3579-b1ec-e31dda01c2ce.jpg[/img]
为了解决这个问题,在此方法上加synchronized关键字,即
public static [color=darkred]synchronized [/color]ThreadSafeSingleton getInstance(){....}
上述实现有performance问题,采用下面方法
3)Double-Check Locking
[img]http://dl.iteye.com/upload/attachment/486588/bbf4c584-144f-389c-83c9-6bf22970b095.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/486592/4283e6ea-de57-3fdd-8e3d-4db17453ee45.jpg[/img]
4)Initialization on demand holder
这个也是线程安全的延迟的单例初始化方式
public class lazyLoadedSingleton{
private LazyLoadedSingleton(){}
[color=darkred] private static class LazyHolder{ //holds the singleton class
private static final LazyLoadedSingleton singletonInstance = new LazyLoadedSIngleton();
}[/color]
public static LazyLoadedSingleton getInstance(){
return LazyHolder.singletonInstance;
}
}
[img]http://dl.iteye.com/upload/attachment/486596/8ff3f928-e510-3451-8c71-56b9e0d4050c.jpg[/img]
5)Singleton的序列化
[img]http://dl.iteye.com/upload/attachment/486598/631a795b-6266-38e7-84ec-a0843393e78b.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/486600/7b505618-6d89-318e-92bf-8679301c072d.jpg[/img]
1)最简单的单例
[img]http://dl.iteye.com/upload/attachment/486570/1ebae9e0-634a-3230-8c85-ceed3ac9ba16.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/486575/6303bf8e-2c27-37e6-a743-d1c416950aab.jpg[/img]
2)延迟创建
延迟实例化单例对象(static属性在加载类时就会被初始化),即在第一次使用该类的实例时才实例化
public class UnThreadSafeSingleton{
private static UnThreadSafeSingleton = null;
public static UnThreadSaceSingleton getInstance(){
if(instance==null){
instance = new UnThreadSafeSingleton();
}
return instance;
}
}
此方法不是线程安全的
[img]http://dl.iteye.com/upload/attachment/486584/6cce968c-1264-3579-b1ec-e31dda01c2ce.jpg[/img]
为了解决这个问题,在此方法上加synchronized关键字,即
public static [color=darkred]synchronized [/color]ThreadSafeSingleton getInstance(){....}
上述实现有performance问题,采用下面方法
3)Double-Check Locking
[img]http://dl.iteye.com/upload/attachment/486588/bbf4c584-144f-389c-83c9-6bf22970b095.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/486592/4283e6ea-de57-3fdd-8e3d-4db17453ee45.jpg[/img]
4)Initialization on demand holder
这个也是线程安全的延迟的单例初始化方式
public class lazyLoadedSingleton{
private LazyLoadedSingleton(){}
[color=darkred] private static class LazyHolder{ //holds the singleton class
private static final LazyLoadedSingleton singletonInstance = new LazyLoadedSIngleton();
}[/color]
public static LazyLoadedSingleton getInstance(){
return LazyHolder.singletonInstance;
}
}
[img]http://dl.iteye.com/upload/attachment/486596/8ff3f928-e510-3451-8c71-56b9e0d4050c.jpg[/img]
5)Singleton的序列化
[img]http://dl.iteye.com/upload/attachment/486598/631a795b-6266-38e7-84ec-a0843393e78b.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/486600/7b505618-6d89-318e-92bf-8679301c072d.jpg[/img]