只生成一个实例的模式被称作单例模式
UML
Singleton
public class Singleton {
private static Singleton singleton = new Singleton();
private Singleton() {
System.out.println("生成了一个实例");
}
public static Singleton getInstance() {
return singleton;
}
}
Main
public class Main {
public static void main(String[] args) {
// write your code here
System.out.println("Start.");
Singleton obj1 = Singleton.getInstance();
Singleton obj2 = Singleton.getInstance();
if (obj1 == obj2) {
System.out.println("obj1 与 obj2是相同的实例.");
} else {
System.out.println("obj1 与 obj2是不同的实例");
}
System.out.println("End.");
}
}