单例就是在系统运行中只有一个实例对象
public class Factory {
private static Factory factory = new Factory();
private Factory(){
System.out.println("--");
}
public static Factory getFactory(){
return factory;
}
public void say(){
System.out.println("say");
}
}
第二种
public class Factory {
private static Factory factory =null;
private Factory(){
System.out.println("--");
}
public synchronized static Factory getFactory(){
if(factory==null){
factory = new Factory();
}
return factory;
}
public void say(){
System.out.println("say");
}
}