论坛首页 招聘求职版 面试秘籍

????????

浏览 468 次
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-07-01 关键字: 面试题目
内容已删除
   
时间:2008-07-02
yujianqiu 写道
问题:请写一段代码说明如何实现Singleton模式?

答案:
public class Singleton {
    private static Singleton instance;

    public static Singleton getInstance() {
        if (null == instance) {
           instance = new Singleton();
        }
        return instance;
    }

    private Singleton() {
    }
}


点评:
在众模式中,Singleton模式是知名度最高的之一(另外一个是“工厂模式”),不过能够完整写出实现的人并不多。实现Singleton模式的要点有两个:
1、静态实例引用,及get方法;
2、私有的默认构造器;
答错的问题都出在第二点——私有的默认构造器,它是实例唯一性的保证。

It is ok if you are not using your code multi-thread environment. So your answer is actually wrong if your program is running in multi-thread environment.

There are actually three solutions which can be used to solve this problem. If you know that, that means you really understand Singleton pattern then.

Hint: Three solutions are related on the performance of the code.
   
0 请登录后投票
时间:2008-07-02
http://www.ibm.com/developerworks/java/library/j-dcl.html
   
0 请登录后投票
时间:2008-07-02
单态都写不出来?呵呵,还有suke说的对,这个在多线程下还是可能出问题的
   
0 请登录后投票
时间:2008-07-02
yujianqiu 写道
问题:请写一段代码说明如何实现Singleton模式?

答案:
public class Singleton {
    private static Singleton instance;

    public static Singleton getInstance() {
        if (null == instance) {
           instance = new Singleton();
        }
        return instance;
    }

    private Singleton() {
    }
}


点评:
在众模式中,Singleton模式是知名度最高的之一(另外一个是“工厂模式”),不过能够完整写出实现的人并不多。实现Singleton模式的要点有两个:
1、静态实例引用,及get方法;
2、私有的默认构造器;
答错的问题都出在第二点——私有的默认构造器,它是实例唯一性的保证。

lz,这样写在多线程下面有问题,只是理论上,出现的可能性很小。建议下面下法:

public class Singleton {
    private static Singleton instance;
    static {
       instance = new Singleton();
    }

    public static Singleton getInstance() {
        return instance;
    }

    private Singleton() {
    }
}
 

 或者对new Singleton()加同步锁,修改如下:

 

public class Singleton {
    private static Singleton instance;

    public synchronized static Singleton getInstance() {
        if (null == instance) {
           instance = new Singleton();
        }
        return instance;
    }

    private Singleton() {
    }
}

 或则

 

public class Singleton {
    private static Singleton instance;

    public static Singleton getInstance() {
       synchronized(Singleton.class) {
         if (null == instance) {
             instance = new Singleton();
          }
       }
        
        return instance;
    }

    private Singleton() {
    }
}

   
0 请登录后投票
时间:2008-07-02
TomHornson 写道
http://www.ibm.com/developerworks/java/library/j-dcl.html


Yes, double-checked locking is one solution to fix the performance problem.

The other solution, which we probably should not do is synchronize the getInstance() method.
This can fix multi-thread problem, but the performance will be the worst.

Another solution is to create a static singleton during the class load time. The problem of this solution is if the singleton needs a lot of resources to create, if in your application runtime, you do not use that singleton at all, you are wasting your time to create it. But actually this is the simplest way to do and most application definitely need create that singleton instance during the runtime.

Anyway, double-checked locking is a recommended solution and has been widely used in multi-thread environment.
   
0 请登录后投票
论坛首页 招聘求职版 面试秘籍

跳转论坛:
JavaEye推荐