<分区>
Possible Duplicate:
Difference between static class and singleton pattern?
Java 哪个更好,
实现公共(public)静态方法,比如
Factory.createLoginRequest()
或者实现单例模式,比如
Factory.getInstance().createLoginRequest()
(两者都将返回一个 Request 对象。)
哪个更好,为什么?
<分区>
Possible Duplicate:
Difference between static class and singleton pattern?
Java 哪个更好,
实现公共(public)静态方法,比如
Factory.createLoginRequest()
或者实现单例模式,比如
Factory.getInstance().createLoginRequest()
(两者都将返回一个 Request 对象。)
哪个更好,为什么?
最佳答案
来自维基百科:
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.
关于java - 静态方法或单例,选择哪一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7329788/