只能存在一个对象实例,只能提供一个取得其对象实例的方法,只能产生一个
对象
单例模式的实现需要
public class Single {
//声明一个静态的私有的引用变量,指向一个仅有对象
private static Single onlyone = new Single();
//声明一个静态的公共的方法,通过调用这个方法获取仅有的对象
public static Single getOnlyone() {
return onlyone;
}
//私有化构造器保证在外部类中无法创建对象
private int count = 1000;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
private Single() {
}
public static void main(String[] args) {
System.out.println(onlyone.count);
}
}