How far back up the implementation hierarchy do I want to go when returning an object?
返回对象时,我希望在实现层次结构中备份多远?
Using the Java collection interface as an example, would this be appropriate?
以Java集合接口为例,这是否合适?
Collection outerMethod() {
return innerMethod();
}
List innerMethod() {
List list = new ArrayList();
//do something with the list that requires a method in the List interface
return list;
}
Or would you want to use List as a return for the outer method?
或者你想使用List作为外部方法的返回?
Another example,
List outerMethod() {
List list = innerMethod();
//do something with the list that requires a method in the List interface
return list;
}
Collection innerMethod() {
return new ArrayList();
}
An example with parameters,
参数示例,
void outerMethod() {
innerMethod((List) innerMethodTwo);
}
void innerMethodOne(List list) {
//do something with the list
}
Collection innerMethodTwo() {
return new ArrayList();
}
Can anyone offer any general advice?
任何人都可以提供任何一般性建
3 个解决方案
#1
Your return type should be as specific as possible to give the consumer of your method the greatest flexibility. In the same spirit it is also best practice to make the types of any parameters as abstract as possible so that consumers of your method also have greater flexibility in what types they can pass in.
您的退货类型应尽可能具体,以便为您的方法的消费者提供最大的灵活性。本着同样的精神,最佳做法是将任何参数的类型设置为尽可能抽象,以便您的方法的使用者在可以传递的类型方面具有更大的灵活性。
#2
I've found that the answer to the question of how specific to be when exposing types from your class depends on your expectations and restrictions for how callers will interact with your class as well as how your code may evolve. It's a complicated balancing act.
我发现在从类中公开类型时具体的问题的答案取决于您对调用者如何与您的类进行交互的期望和限制以及您的代码可能如何演变。这是一个复杂的平衡行为。
For instance, if you expect callers will need to access elements by index you may return List
, but if not then Collection
may be sufficient. If you want to restrict callers to only be able iterate over the results and not change them - you want want to return an Iterable<>
wrapper instead. If you know that your internal representation may change, you may want to avoid returning very specific classes like List, so that consumers can be more decoupled from your implementation.
例如,如果您希望调用者需要按索引访问元素,则可以返回List,但如果没有,那么Collection可能就足够了。如果您希望限制调用者只能迭代结果而不更改它们 - 您希望返回一个Iterable <>包装器。如果您知道您的内部表示可能会发生变化,您可能希望避免返回像List这样的特定类,以便消费者可以更加脱离您的实现。
Answering these questions is not easy - there is no right or wrong answer - there's also no perfect guidelines. It takes experience, thought, and intuition. And (like most of us), you'll probably get it wrong.
回答这些问题并不容易 - 没有正确或错误的答案 - 也没有完美的指导方针。它需要经验,思想和直觉。而且(像我们大多数人一样),你可能会弄错。
I will say, that personally, I err on the side of being more restrictive in what I expose from my code - especially when it is immature and not yet widely used. Sometimes you can go back and change the return types in your public interface ... sometimes you can't.
我会说,就个人而言,我错误地认为我从代码中暴露出更多限制 - 特别是当它不成熟而且还没有被广泛使用时。有时您可以返回并更改公共界面中的返回类型...有时您不能。
#3
I think this is a more general version of this question. The general advice I gave is that there are two rules I follow:
我认为这是这个问题的更一般的版本。我给出的一般建议是我遵循两条规则:
- Accept the most basic type that will work
- Return the richest type your user will need
接受最有效的基本类型
返回您的用户需要的最丰富的类型
#1
Your return type should be as specific as possible to give the consumer of your method the greatest flexibility. In the same spirit it is also best practice to make the types of any parameters as abstract as possible so that consumers of your method also have greater flexibility in what types they can pass in.
您的退货类型应尽可能具体,以便为您的方法的消费者提供最大的灵活性。本着同样的精神,最佳做法是将任何参数的类型设置为尽可能抽象,以便您的方法的使用者在可以传递的类型方面具有更大的灵活性。
#2
I've found that the answer to the question of how specific to be when exposing types from your class depends on your expectations and restrictions for how callers will interact with your class as well as how your code may evolve. It's a complicated balancing act.
我发现在从类中公开类型时具体的问题的答案取决于您对调用者如何与您的类进行交互的期望和限制以及您的代码可能如何演变。这是一个复杂的平衡行为。
For instance, if you expect callers will need to access elements by index you may return List
, but if not then Collection
may be sufficient. If you want to restrict callers to only be able iterate over the results and not change them - you want want to return an Iterable<>
wrapper instead. If you know that your internal representation may change, you may want to avoid returning very specific classes like List, so that consumers can be more decoupled from your implementation.
例如,如果您希望调用者需要按索引访问元素,则可以返回List,但如果没有,那么Collection可能就足够了。如果您希望限制调用者只能迭代结果而不更改它们 - 您希望返回一个Iterable <>包装器。如果您知道您的内部表示可能会发生变化,您可能希望避免返回像List这样的特定类,以便消费者可以更加脱离您的实现。
Answering these questions is not easy - there is no right or wrong answer - there's also no perfect guidelines. It takes experience, thought, and intuition. And (like most of us), you'll probably get it wrong.
回答这些问题并不容易 - 没有正确或错误的答案 - 也没有完美的指导方针。它需要经验,思想和直觉。而且(像我们大多数人一样),你可能会弄错。
I will say, that personally, I err on the side of being more restrictive in what I expose from my code - especially when it is immature and not yet widely used. Sometimes you can go back and change the return types in your public interface ... sometimes you can't.
我会说,就个人而言,我错误地认为我从代码中暴露出更多限制 - 特别是当它不成熟而且还没有被广泛使用时。有时您可以返回并更改公共界面中的返回类型...有时您不能。
#3
I think this is a more general version of this question. The general advice I gave is that there are two rules I follow:
我认为这是这个问题的更一般的版本。我给出的一般建议是我遵循两条规则:
- Accept the most basic type that will work
- Return the richest type your user will need
接受最有效的基本类型
返回您的用户需要的最丰富的类型