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

标签 java android design-patterns static singleton

<分区>

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/

相关文章:

java - 我应该如何清理 Java 中的数据库输入?

java - Android studio 字符串参数错误

c# - 用更好的图案替换铸件

java - HashMap 数据始终有新数据

Java:将公共(public)类方法公开给同一项目的所有包,但对其他项目私有(private)

JavaFX、FXML 和 Controller : switching scenes, 维护场景数据

javascript - 使用 GWT 编辑器框架创建包含 CellList 的模型?

JAVA:如何在嵌套的 JSONString 中查找子字符串的所有匹配项并替换为一些文本

android - 九补丁StateListDrawable渲染问题

Android从webservice获取图片,如何?