题目:设计一个类,我们只能生成该类的一个实例。
代码如下:
package demo;
public class TestSingleton {
/**
* 懒汉式,线程安全
*/
public static class Singleton1 {
private static final Singleton1 INSTANCE = new Singleton1();
private Singleton1() {
}
public static Singleton1 getInstance() {
return INSTANCE;
}
}
/**
* 懒汉式,变种,线程安全
*/
public static class Singleton2 {
private static Singleton2 instance = null;
static {
instance = new Singleton2();
}
private Singleton2() {
}
public static Singleton2 getInstance() {
return instance;
}
}
/**
* 饿汉式,线程不安全
*/
public static class Singleton3 {
private static Singleton3 instance = null;
private Singleton3() {
}
public static Singleton3 getInstance() {
if(instance == null) {
instance = new Singleton3();
}
return instance;
}
/**
* 饿汉式,线程安全,多线程环境下效率不高
*/
public static class Singleton4 {
private static Singleton4 instance = null;
private Singleton4() {
}
public static synchronized Singleton4 getInstance() {
if(instance == null) {
instance = new Singleton4();
}
return instance;
}
}
/**
* 使用静态内部类,线程安全
*/
public static class Singleton5 {
private static final class SingletonHolder {
private static final Singleton5 INSTANCE = new Singleton5();
}
private Singleton5() {
}
public static Singleton5 getInstance() {
return SingletonHolder.INSTANCE;
}
}
/**
* 静态内部类,使用双重校验锁,线程安全
*/
public static class Singleton6 {
private volatile static Singleton6 instance = null;
private Singleton6() {
}
public static Singleton6 getInstance() {
if(instance == null) {
synchronized (Singleton6.class) {
if(instance == null) {
instance = new Singleton6();
}
}
}
return instance;
}
}
/**
* 静态内部类,使用枚举方式,线程安全
*/
public enum Singleton7 {
INSTANCE;
public void whateverMethon() {
}
}
public static void main(String[] args) {
System.out.println(Singleton1.getInstance() == Singleton1.getInstance());
System.out.println(Singleton2.getInstance() == Singleton2.getInstance());
System.out.println(Singleton3.getInstance() == Singleton3.getInstance());
System.out.println(Singleton4.getInstance() == Singleton4.getInstance());
System.out.println(Singleton5.getInstance() == Singleton5.getInstance());
System.out.println(Singleton6.getInstance() == Singleton6.getInstance());
System.out.println(Singleton7.INSTANCE == Singleton7.INSTANCE);
}
}
}