简体   繁体   English

是否有两个版本的相同方法只有不同的签名(方法名称和'throws'属性)被认为是糟糕的设计?

[英]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()

  1. 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样板代码。

  2. 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.

相关问题 当实现仅在单个方法中有所不同时,建议使用哪种设计模式? - Which design pattern is recommended when implementations only differ in a single method? Java 8方法签名相同方法的不同版本 - Java 8 Method Signature different versions of same method “外观方法”(称为本类的公共方法)是否被视为不良设计? - Is 'facade method' (calling public methods of own class) considered bad design? 如何创建两个具有相同名称,签名和返回类型的方法的类,就像它们实现相同的接口一样 - How to make two classes which both have method with the same name, signature and return type behave like they would implement the same interface 我们是否需要在throw声明中在方法签名中已经提到的catch块中引发相同的异常 - Do we need to throw the same exception in catch block which is already mentioned in method signature using throws declaration 为什么Java在实现2个接口时只允许单个方法X,这两个接口都声明X但是它们的throws子句有所不同? - Why does Java allow only a single method X when implementing 2 interfaces that both declare X but which differ in their throws clauses? 两个具有相同包名,相同类名和相同方法名的罐子 - Two jars having same package name, same class name and same method name 如何设计此方法的签名 - How to design the signature of this method 当一个类实现两个具有相同方法签名但修饰符default和static不同的接口时会发生什么 - what will happen when a class implement two interface having same method signature but different modifier default and static 如何重构仅在调用 java 中的对象时调用的方法不同的方法? - How to refactor methods that only differ in which method they call on an object in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM