[英]Is having two versions of the same method which only differ in their signature (method name and 'throws' attribute) considered bad design?
I would like to design an API where I have two versions of the same method, extractLastElement()
: 我想设计一个API,其中我有两个版本的相同方法,
extractLastElement()
:
First version will not have a throws attribute: Object extractLastElementSafe();
第一个版本没有throws属性:
Object extractLastElementSafe();
Will be used when the client 'knows for sure' there are elements inside the collection, for example he've just added some elements, so no need for try-catch boilerplate code. 将在客户'确切知道'集合中有元素时使用,例如他刚刚添加了一些元素,因此不需要try-catch样板代码。
Second version will throw a Checked Exception: Object extractLastElement() throws NoMoreElementsException
; 第二个版本将抛出Checked Exception:
Object extractLastElement() throws NoMoreElementsException
; Will be used by the client when he isn't sure if there are still elements left in the collection, for example inside a loop. 当客户不确定集合中是否还有元素时,例如在循环内部,将由客户端使用。
Is this considered bad design? 这被认为是糟糕的设计吗? Is there any alternative for emulating this behavior?
有没有其他方法来模仿这种行为?
public class SomeCollection {
private List<String> arr;
public SomeCollection(List<String> arr) {
this.arr = arr;
}
public String extractLastElementSafe() {
return arr.remove(arr.size() - 1);
}
public String extractLastElement() throws NoElementsLeftException {
try {
return arr.remove(arr.size() - 1);
} catch (IndexOutOfBoundsException e) {
throw new NoElementsLeftException(e); // throwing a checked exception
}
}
}
class NoElementsLeftException extends Exception {
public NoElementsLeftException(Throwable cause) {
super(cause);
}
}
It is always questionable to give users the ability to do the same thing in different ways. 让用户以不同的方式做同样的事情总是有问题的。 It is also not elegant to force the client to worry about that last element being there or not.
强迫客户担心最后一个元素是否存在也是不优雅的。
But in the end, that is more of a style thing, and your approach can be regarded okay. 但最后,这更像是一种风格的东西,你的方法可以被认为是好的。 Nonetheless you expose your clients to call the "safe" method and to end up with a runtime exception.
尽管如此,您还是要让客户端调用“安全”方法并最终得到运行时异常。 So there are actually two different error scenarios in the end.
所以最终实际上有两种不同的错误情况。
The one subtle thing to definitely change: avoid the code duplication! 绝对改变的一个微妙的事情:避免代码重复! The method that does the try/catch to throw that exception should simply invoke the "safe" method!
执行try / catch抛出该异常的方法应该只是调用“安全”方法!
Having said all of that: I would personally offer only one method that throws some runtime exception. 说完所有这些:我个人只提供一种抛出一些运行时异常的方法。 Standard Java collections use unchecked exceptions, and so should you.
标准Java集合使用未经检查的异常,您也应如此。
To quote Robert Martin: "the war between checked and unchecked exceptions is over, and unchecked won". 引用罗伯特·马丁的话说:“已检查和未经检查的例外之间的战争结束了,并且未经检查就赢了”。 And he wrote that more than 10 years ago.
他在十多年前就写过这篇文章。
If you have well-defined usage scenarios for each version of the method, it makes sense to have both. 如果您为方法的每个版本都有明确定义的使用方案,那么两者都有意义。 An example of such a design decision can be found in class Queue , which works in two modes when eg, trying to remove an element from an empty queue: one method (
poll
) returns null, and another ( remove
) throws an exception. 这种设计决策的一个例子可以在Queue类中找到,它在两种模式下工作,例如,尝试从空队列中删除一个元素:一个方法(
poll
)返回null,另一个( remove
)抛出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.