请问单态模式什么时候用,我以前看JAVA基础也没有看到讲过他,请大家发言,谈谈自己的看法,代码如下:
//单态模式测试类
public class SingletonTest {
   //该类的一个普通属性
int value;
//使用静态属性类保存该类的一个属性
private static SingletonTest instance;
    //构造器私有化,避免该类被多次实例
private SingletonTest(){
System.out.println("正在执行构造器....");
}
//提供静态方法来返回该类的实例
public static SingletonTest getInstance(){
//实例化类实例前,先检查该类的的实例是否存在
if(instance==null){
//如果不存在,则新建一个实例
instance = new SingletonTest();
}
//返回该类的成员变量:该类的实例
return instance;

}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub

SingletonTest t1 = SingletonTest.getInstance();
SingletonTest t2 = SingletonTest.getInstance();
t1.setValue(20);
t2.setValue(0);
System.out.println(t1);
System.out.println(t2); }
}

解决方案 »

  1.   

    这是最简单,最常见的一种设计模式了,它保证了在你的Application里面只有该类的唯一一个实例。
      

  2.   

    看一下 《java与模式》,对你会有很大帮助
      

  3.   

    主要用于减少内存占用和节省JVM消耗。一个类的方法,如果在任何状态,被任何其他对象调用都能互不影响,则可以采用单态模式。比如数据源,以及一些标准服务器。
      

  4.   

    单例的各种写法,大家一起来研究下啊!有的直接暴露静态成员变量、有的用静态方法getInstance(),有的还用if判断下。
      

  5.   

    我比较在意Java如何用泛型来表达Singleton,A类是Singleton,B也是Singleton,其实它们有很多代码是一致的。那就需要泛型把它们表达起来。
    可惜,我不会Java的泛型
      

  6.   

    楼主的单态是有点瑕疵的。单态可以两种初始化方法:
    第一种:public class SingletonTest {
    int value;
    private static SingletonTest instance; private SingletonTest() {
    System.out.println("正在执行构造器....");
    } public synchronized static SingletonTest getInstance() {   //注意这行,必须要同步一下
    if (instance == null)
    instance = new SingletonTest();
    return instance; } public int getValue() {
    return value;
    } public void setValue(int value) {
    this.value = value;
    } public static void main(String[] args) {
    SingletonTest t1 = SingletonTest.getInstance();
    SingletonTest t2 = SingletonTest.getInstance();
    t1.setValue(20);
    t2.setValue(0);
    System.out.println(t1);
    System.out.println(t2);
    }
    }
    第二种:public class SingletonTest {
    int value;
    private static SingletonTest instance = new SingletonTest(); private SingletonTest() {
    System.out.println("正在执行构造器....");
    } public synchronized static SingletonTest getInstance() {   //注意这行
    return instance; } public int getValue() {
    return value;
    } public void setValue(int value) {
    this.value = value;
    } public static void main(String[] args) {
    SingletonTest t1 = SingletonTest.getInstance();
    SingletonTest t2 = SingletonTest.getInstance();
    t1.setValue(20);
    t2.setValue(0);
    System.out.println(t1);
    System.out.println(t2);
    }
    }
      

  7.   

    第二种应该是这样的:public class SingletonTest {
    int value;
    private static SingletonTest instance = new SingletonTest();   //注意这行 private SingletonTest() {
    System.out.println("正在执行构造器....");
    } public static SingletonTest getInstance() {
    return instance; } public int getValue() {
    return value;
    } public void setValue(int value) {
    this.value = value;
    } public static void main(String[] args) {
    SingletonTest t1 = SingletonTest.getInstance();
    SingletonTest t2 = SingletonTest.getInstance();
    t1.setValue(20);
    t2.setValue(0);
    System.out.println(t1);
    System.out.println(t2);
    }
    }
      

  8.   

    用Java自己写个简单的画图板,你会把重要的设计模式都碰到的,自己设计。
    这是WPS开发人员最基本的要求。