单例模式分为:饿汉式和懒汉式
类图:
饿汉式:
public class SingleBitmap {
/**
* 饿汉式
*/
private static SingleBitmap sSingleBitmap = new SingleBitmap();
private SingleBitmap() {
}
/**
* 饿汉式
* @return
*/
public static SingleBitmap getInstance() {
if (sSingleBitmap != null) {
return sSingleBitmap;
}
return null;
}
}
懒汉式:
public class SingleBitmap {
/**
* 懒汉式
*/
private static SingleBitmap sSingleBitmap;
private SingleBitmap() {
}
/**
* 懒汉式
* @return
*/
public static SingleBitmap getInstance() {
if (sSingleBitmap == null) {
synchronized (SingleBitmap.class) {
if (sSingleBitmap == null) {
sSingleBitmap = new SingleBitmap();
}
}
}
return sSingleBitmap;
}
}
注:这博客写的不错,值得参考
http://blog.csdn.net/jason0539/article/details/23297037