关于维基中singleton pattern的一段

Another notable difference is that static member classes cannot implement an interface , unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton.

以上摘自:http://en.wikipedia.org/wiki/Singleton_pattern

他说静态类实例和singleton的区别是静态成员类不能实现接口,感觉不对啊,可以实现吧,他说除非是个marker,那意思是去实现的话就不能是个静态成员类了?就得是个单例了?单例就允许实现接口,这算什么定义呢...

貌似如此也无不可

Java code
interface Foo {
    void foo();
}

class Outer {
    static class Inner implements Foo {
        public void foo() {
        }
    }
}
 



维基一般不会错吧,那到底哪里有问题呢?

 

 

 

 

关于这个,仔细看了很久,焦点貌似应该集中在下面这段:

Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed. Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton.

static member classes: 如果仅从字面理解,我觉得应是静态成员类没错,虽然前面比的是static instance of a class,两者不同,但这里主要比较的是singleton

接下来的表述就非常迷惑了,或者干脆可以断定为不完整,比如
static member classes cannot implement an interface
如果非要实现怎么办呢?后面也没说接口必须按照什么规范

 

interface Foo { 
    void hello(); 
} 

class Outer { 
    static class Inner implements Foo { 
        public void hello() { 
            System.out.println("hello"); 
        } 
    } 
}

 

 

接口的确是实现了,但它就必须得是singleton吗?明显不是啊,或者static member classes理解有误,不过感觉也不太像

 

那假如把static member classes理解成静态类(classes which have only static members),貌似像说得通

 

interface Foo {
    void hello();
}

class Outer implements Foo {    
    
    public static void go() {
        
    }

    @Override
    public void hello() {
        
    }
    
    public static void main(String[] args) {
        Outer.go();
    }
    
}
 

像这样如果实现了接口,就出现了非静态的方法了,除非是个marker,否则无以为静态类。因此在两者的比较环境中非此即彼,能去实现非marker接口的就算是singleton了,不知这种逻辑算勉强是对这个的解释否

 

from: http://topic.csdn.net/u/20110221/21/01c7c6a7-295d-4c3e-bfb4-aa3ece3b7275.html

 

something also has proved it.

 

There are a few ways to implement Singletons. Although you can get Singleton-like behavior with static fields and methods [for example, java.lang.Math.sin(double) ], you gain more flexibility by creating an instance. With Singletons implemented as single instances instead of static class members, you can initialize the Singleton lazily, creating it only when it is first used. Likewise, with a Singleton implemented as single instance, you leave open the possibility of altering the class to create more instances in the future. With some implementations of the Singleton, you allow writers of subclasses to override methods polymorphically, something not possible with static methods.

 

from: http://java.sun.com/developer/technicalArticles/Programming/singletons/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值