简体   繁体   English

在valueOf()和newInstance()之间选择的标准是什么?

[英]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. valueOfacquire意味着您将获得包含相同信息的任何实例。

newInstance or create implies you will get a different instance every time. newInstancecreate意味着每次都会得到一个不同的实例。

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. 在您的情况下, newInstancecreate是合适的。

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.

相关问题 使用.newInstance()和不使用?有什么区别? - What is the difference between using .newInstance() and not using? “ new A()”和“ A.newInstance()”有什么区别? - What is the difference between “new A()” and “A.newInstance()”? “Class.forName()”和“Class.forName().newInstance()”有什么区别? - What is the difference between “Class.forName()” and “Class.forName().newInstance()”? Array.newInstance和直接初始化有什么区别? - What is the difference between Array.newInstance and direct initialization? new运算符和Class.newInstance()之间有什么区别? - What is the difference between the new operator and Class.newInstance()? String.valueOf(String Object)的null和“null”有什么区别 - what is difference between null and “null” of String.valueOf(String Object) 我的系统选择加密算法的标准是什么 - what is the criteria to choose encryption algorithm for my system Integer.valueOf() 和 Autoboxing 之间的性能差异是什么 - What is performance difference between Integer.valueOf() and Autoboxing bindClass和newInstance之间的区别 - Difference between bindClass and newInstance BitSet valueOf是什么? - BitSet valueOf does what?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM