版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/eyishion/article/details/81906339
这个模式是最简单的,用它最主要是为了减少对象的创建,网上资料一大把,相信只要是开发,闭着眼睛都能写出它来;
分两种方式,恶汉式和饱汉式;
public class A {
private A() {
}
//饿汉式
private static final A ourInstance = new A();
private static A inInstance;
//饱汉式
public static A getInstance() {
if (inInstance == null) {
synchronized (A.class) {
if (inInstance == null) {
inInstance = new A();
}
}
}
return inInstance;
}
}