java 静态方法或单例,该选择哪一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7329788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:32:39  来源:igfitidea点击:

Static methods or Singleton, which one to choose?

javaandroiddesign-patternsstaticsingleton

提问by Mohammad Ersan

Possible Duplicate:
Difference between static class and singleton pattern?

可能的重复:
静态类和单例模式之间的区别?

Which is better in Java,

哪个在Java中更好,

implementing public static methods, like

实现公共静态方法,比如

Factory.createLoginRequest()

or implementing Singleton pattern, like

或者实现单例模式,比如

Factory.getInstance().createLoginRequest()

(Boths will return a Request object.)

(两者都将返回一个 Request 对象。)

Which one is better and why?

哪个更好,为什么

采纳答案by Mahdi Hijazi

from wikipedia:

来自维基百科:

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.

请注意类的简单静态实例和单例之间的区别:虽然单例可以作为静态实例实现,但它也可以懒惰地构造,直到需要时才需要内存或资源。另一个显着区别是静态成员类不能实现接口,除非该接口只是一个标记。所以如果这个类要实现一个由接口表达的契约,它真的必须是一个单例。

回答by Arnout Engelen

It depends.

这取决于。

Choose singletons, because:

选择单例,因为:

  • In general, I'd say a singleton is slightly neater because it allows you to do some initialization of the singleton object in/from the (private) constructor.
  • When it is later decided this object should no longer be a singleton (due to new insights or new requirements), it is easier to refactor it: you just need to change all the places that get the instance, instead of all calls to the static methods.
  • 一般来说,我会说单例稍微整洁,因为它允许您在(私有)构造函数中/从(私有)构造函数中对单例对象进行一些初始化。
  • 当后来决定这个对象不再是单例时(由于新的见解或新的要求),重构它会更容易:您只需要更改获取实例的所有位置,而不是所有对静态的调用方法。

Use static methods, because:

使用静态方法,因为:

  • In the specific case of android, you might prefer static methods for performance - I suspect calling a static function might be a bit faster (easier to optimize for the compiler) compared to calling a method on a singleton object.
  • 在 android 的特定情况下,您可能更喜欢使用静态方法来提高性能 - 我怀疑与在单例对象上调用方法相比,调用静态函数可能会更快一些(更容易为编译器优化)。

回答by Andrey Atapin

It depends. Singleton concept considers a limitation on object initialization. In other words, singleton object must be the onlyinstance of a singleton class in the runtime. But if you just need to create objects of some family of classes, then go with factory method pattern.

这取决于。单例概念考虑了对象初始化的限制。换句话说,单例对象必须是运行时中单例类的唯一实例。但是如果你只需要创建一些类家族的对象,那么就使用工厂方法模式。

回答by Frank Grimm

I'd go for the Singleton because it allows you to use Java's object-oriented features like inheritance (method overriding) which won't work for static methods.

我会选择 Singleton,因为它允许您使用 Java 的面向对象的功能,例如继承(方法覆盖),这些功能不适用于静态方法。