Java单态模式例程
class Single {
private static Single s = new Single();
//单态模式设计,这里的构造函数是私有的,所以不能new
private Single(){}
//只能通过调用getInstance()得到Single的引用
public static Single getInstance(){
return s;
}
public void print(){
System.out.println("Hello Java");
}
}
public class Test {
public static void main(String args[]){
Single s1 = null;
s1 = Single.getInstance();
s1.print();
Single s2 = Single.getInstance();
System.out.println(s2);
System.out.println(s1);
}
}
欢迎访问:wenhao.ink