简体   繁体   English

使用什么异常来防止方法被多次调用?

[英]What exception to use to prevent a method from being called multiple times?

I have a method that should only be called once during an object's lifetime. 我有一个只能在对象生命周期内被调用一次的方法。 In order to ensure that this is the case, the method sets a boolean flag in the Object to true so it can later check if this method has already run. 为了确保是这种情况,该方法将Object中的boolean标志设置为true以便以后可以检查此方法是否已经运行。 I am currently throwing an IllegalArgumentException (with a descriptive message) if this method is called a second time during a single object's lifetime, but that doesn't feel quite right to me, since the problem is not actually with the arguments themselves. 如果在单个对象的生命周期中第二次调用此方法,则当前会抛出IllegalArgumentException (带有描述性消息),但这对我来说并不完全正确,因为问题实际上并不在于参数本身。 Is there a better exception to use than an IllegalArgumentException ? 是否有比IllegalArgumentException更好的异常使用?

I chose not to use an assert statement in this case, because the class and method are both visible outside the package, so the problem may be caused by code outside of my package. 在这种情况下,我选择不使用assert语句,因为类和方法在程序包外部都是可见的,因此问题可能是由程序包外部的代码引起的。 Is that correct thinking? 那是正确的想法吗?

Throw an IllegalStateException . 引发IllegalStateException

But since exceptions shouldn't be part of the ordinary control flow, you should add a companion method, which returns a boolean that indicates whether the next call to the method will be successful. 但是由于异常不应成为普通控制流的一部分,因此您应该添加一个伴随方法,该方法返回一个布尔值,该布尔值指示下一次对该方法的调用是否将成功。

An example for such a companion method is Iterator#hasNext() . 这种伴随方法的一个示例是Iterator#hasNext()

A well-designed API must not force its clients to use exceptions for ordinary control flow. 设计良好的API不得强迫其客户端对常规控制流使用异常。 A class with a “state-dependent” method that can be invoked only under certain unpredictable conditions should generally have a separate “state-testing” method indicating whether it is appropriate to invoke the state-dependent method. 具有“状态依赖”方法的类只能在某些不可预测的条件下调用,通常应具有单独的“状态测试”方法,指示是否适合调用状态依赖方法。 For example, the Iterator interface has the state-dependent method next and the corresponding state-testing method hasNext. 例如,Iterator接口具有与状态有关的方法next,而相应的状态测试方法具有next。 1 1

1: from Effective Java, Chapter 9: Exceptions 1:来自有效的Java,第9章:异常

What should worry you more than the specific exception type is the fact that you created a bad design here. 除了特定的异常类型之外,您还需要担心的是您在此处创建了错误的设计。

Good interfaces make it easy to do the right thing and hard to do the wrong thing. 良好的界面使执行正确的事情变得容易,而错误的事情则很难。

Meaning: your current implementation makes it easy to call that method twice; 含义:您当前的实现可以轻松地两次调用该方法; respectively you now force your clients to always check if that method was already called. 您现在分别迫使客户始终检查该方法是否已经被调用。

So, instead of spending your time on the exception type: step back and figure how to dissect your one class into two classes for example. 因此,不要花时间在异常类型上:倒退并确定如何将一个类分解为两个类。 And find a nice so that calling that specific method gives you a different object to work on. 并且找到一个不错的方法,以便调用该特定方法可以为您提供一个不同的对象来进行处理。 Or check if you should rather use a state machine to solve this problem. 或检查是否应该使用状态机来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM