[英]What is the criteria to choose between valueOf() and newInstance()?
Suppose I have a class ObjectInfo which contains Object name & Object type as String.(I am just cooking up something for the sake of asking the question.) 假设我有一个ObjectInfo类,其中包含Object name和Object type作为String。(我只是为了提出问题而做点什么。)
class ObjectInfo {
String objectName;
String objectType;
private ObjectInfo(String objectName, String objectType) {
this.objectName = objectName;
this.objectType = objectType;
}
}
And If I want to provide a static factory method to creating instances of this class, which of the following two methods is better & why? 如果我想提供一个静态工厂方法来创建这个类的实例,以下两种方法中的哪一种更好?为什么?
public static ObjectInfo newInstance(String objectName, String objectType) {
return new ObjectInfo(objectName, objectType)
}
public static ObjectInfo valueOf(String objectName, String objectType) {
return new ObjectInfo(objectName, objectType)
}
Basically, what I want to ask is when we should use valueOf() & when newInstance() ? 基本上,我想问的是什么时候我们应该使用valueOf()&newInstance()? Is there any conventions among the programmer's community?
程序员社区中是否有任何约定?
-Ankit -Ankit
There is no official standard, but this is my opinion, 没有官方标准,但这是我的意见,
valueOf
or acquire
implies that you will get any instance which holds the same information. valueOf
或acquire
意味着您将获得包含相同信息的任何实例。
newInstance
or create
implies you will get a different instance every time. newInstance
或create
意味着每次都会得到一个不同的实例。
get
implies you will get an instance if it exist, or null
if it does not. get
暗示如果它存在,你将得到一个实例,如果不存在则得到null
。
In your case newInstance
or create
is appropriate. 在您的情况下,
newInstance
或create
是合适的。
cf 比照
Integer.valueOf(1) == Integer.valueOf(1) // you get the same object
Integer.valueOf(-200) != Integer.valueOf(-200) // you get a different object.
In general, in java, String.valueOf() is called when a string representation of some other type of data is needed. 通常,在java中,当需要某些其他类型数据的字符串表示时,将调用String.valueOf()。 In the case of primitive wrappers (Integer, Double), valueOf() takes a String ("12") and creates an instance of that wrapper.
在原始包装器(Integer,Double)的情况下,valueOf()接受一个String(“12”)并创建该包装器的实例。
In your case, you try to create a new object using a number of parameters. 在您的情况下,您尝试使用许多参数创建新对象。 That is a factory method.
这是一种工厂方法。 It makes more sense to call it newInstance.
称之为newInstance更有意义。
You answered your own question when you said And If I want to provide a static factory method to creating instances of this class
new Instance makes sense in this context (even if both valueOf and new do the same). 你说的时候回答了你自己的问题
And If I want to provide a static factory method to creating instances of this class
新的Instance在这个上下文中是有意义的(即使valueOf和new都做同样的事情)。 To me valueOf (as the name suggests )makes sense when you want to retrieve some meaningful state information from an existing object (not necessarily the entire object state) where as new
is creating brand new instance. 对我来说,当你想要从现有对象(不一定是整个对象状态)中检索一些有意义的状态信息时,valueOf(顾名思义)是有意义的,因为
new
的创建全新实例。
public static ObjectInfo newObjectInfo(String objectName, String objectType)
For a static factory method, I would use the above naming convention. 对于静态工厂方法,我将使用上面的命名约定。 This is useful if the method consumers want to use static imports:
如果方法使用者想要使用静态导入,这很有用:
import static foo.ObjectInfo.newObjectInfo;
//....
ObjectInfo info = newObjectInfo(foo, bar);
You can see this pattern in the Guava API . 您可以在Guava API中看到此模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.