singleton模式的实现方法有很多种么?

除了板桥写的,我在developerwork上还看到了用静态方法和静态变量实现
Singleton模式,另外,theserverside上也有。
就拿板桥的
public class Singleton {
  private static Singleton instance = null;

  public static synchronized Singleton getInstance() {

  //这个方法比上面有所改进,不用每次都进行生成对象,只是第一次     
  //使用时生成实例,提高了效率!
  if (instance==null)
    instance=new Singleton();
  return instance;   }

}

developerworks上类似的为
public class Singleton {
private static Singleton s;
private Singleton(){};
/**
* Class method to access the singleton instance of the class.
*/
public static Singleton getInstance() {
if (s == null)
s = new Singleton();
return s;
}
}

不知道现在究竟有多少种定义,是不是可以自己修改,哪几种是比较为大家公认的。
我还想知道Singleton的最原始的定义是什么,我知道自己还没有理解这个模式的本质,各位朋友帮帮忙吧,谢谢

常用的就是我说的第二种,其他一般可以不考虑。

除了上面提到的两种以外,我看到还有一种实现的方式,下面是原文(很抱歉,我觉得我翻译不好这一段,看它的原文会更加准确一些)

Static Classes as Singleton Patterns

There already is a kind of Singleton class in the standard Java class libraries:the Math class. This is a class that is declared final and all methods are declared static, meaning that the class cannot be extended. The purpose of the Math class is to wrap a number of common mathematical functions such as sin and log in a class-like structure, since the Java language does not
support functions that are not methods in a class. You can use the same approach to a Singleton pattern, making it a final class. You can’t create any instance of classes like Math, and can only call the static methods directly in the existing final class.

final class PrintSpooler
{
//a static class implementation of Singleton pattern
static public void print(String s)
{
System.out.println(s);
}
}
//==============================
public class staticPrint
{
public static void main(String argv[])
{
Printer.print("here it is");
}
}

One advantage of the final class approach is that you don’t have to wrap things in awkward try blocks. The disadvantage is that if you would like to drop the restrictions of Singleton status, this is easier to do in the exception style class structure. We’d have a lot of reprogramming to do to make the
static approach allow multiple instances.

我不认为Math是singleton模式,呵呵

同意 richardluopeng。
Singleton 是单实例,而 Math 是无实例。

在effective java里面讲到静态工厂方法的时候还有一种
就是把类的实例设为final

//Singleton with static factory
public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}

public static Elvis getInstance() {
return INSTANCE;
}
... // Remainder omitted
}

这一段话不是很明白:
One advantage of the final class approach is that you don’t have to wrap things in awkward try blocks. The disadvantage is that if you would like to drop the restrictions of Singleton status, this is easier to do in the exception style class structure. We’d have a lot of reprogramming to do to make the
static approach allow multiple instances.
哪一位能否讲解一下.谢谢。

mochow 说的方法好吧,也适合多线程
要是单线程无所谓,要是多线程就要用mochow 说的这种了吧
是应该这样理解吗

是亚,我说的那种被称为饿汉式,前面bang(?)所提倡的那种一般被
称为懒汉式的。
两者相比较而言各有千秋,但是饿汉式的单例模式更符合java语言的特点

也只适合于java语言

那是不是使用的时候,单线程就用懒汉式,多线程就用恶汉式就可以了呢,那为什么不所有情况都用恶汉式岂不简单,他们有什么区别呢

懒汉式得当然适合于单线程了。
也适合于多线程

呵呵,写错了
本论坛怎么不能编辑文章呀?

应该是饿汉式得单线程和多线程都适合。

懒汉式得,同步一下我想也一样适合与多线程了

TO: mochow
我觉得在懒汉式中,Instance 可以不是final的.
你可以解释一下instance一定final的必要性么?

用final更好,编译器在处理final变量的时候会做优化处理

使用final的原因是,再多线程时如果 public static foo = new FOO()
然后Foo getInstance(),会在每一个线程中创建实例,
但使用final将会在多线程中使用唯一的一个实例!!!!!