断言单例对象不为空
是否有必要对单例对象进行 Assert.notNull ?
我有一个类:
public class ComponentFactory {
private static LibraryFrame libraryFrame;
public static synchronized LibraryFrame getLibraryFrame() {
if (libraryFrame == null) {
libraryFrame = new LibraryFrame();
}
return libraryFrame;
}
}
现在需要使用为:
LibraryFrame libraryFrame = ComponentFactory.getLibraryFrame();
Assert.notNull(libraryFrame);
// other part
这里断言类是org.springframework.util.Assert。
如果断言失败,是否可以在失败发生后调用 System.exit(0) ?
Is it necessary to Assert.notNull of a Singleton Object?
I have a class:
public class ComponentFactory {
private static LibraryFrame libraryFrame;
public static synchronized LibraryFrame getLibraryFrame() {
if (libraryFrame == null) {
libraryFrame = new LibraryFrame();
}
return libraryFrame;
}
}
Now is it needed to use as:
LibraryFrame libraryFrame = ComponentFactory.getLibraryFrame();
Assert.notNull(libraryFrame);
// other part
Here Assert class is org.springframework.util.Assert.
If Assertion failed is there anyway to call System.exit(0) after failure occurred?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Assert 不是必需的,因为
LibraryFrame
实例将始终在此时初始化。The Assert is not necessary since the
LibraryFrame
instance will always be initialized at this point.根据该类的文档,它的真正目的是由方法使用来验证其参数。 (也就是说,它的目的是执行先决条件。)您应该对 ComponentFactory 进行单元测试,并且它们应该断言其静态 getLibraryFrame()< /code> 方法不会返回
null
,但不值得在运行时断言这一点。According to the documentation for that class, it's really intended to be used by a method to valid its arguments. (That is, it's intended for the enforcement of preconditions.) You should have unit-tests for
ComponentFactory
, and they should assert that its staticgetLibraryFrame()
method doesn't returnnull
, but it's not worthwhile to assert this at run-time.当您使用 JUnit 中的 Assert 类时,其中的任何方法都是 void,但它使用 JUnit 框架来告诉您测试是否通过或失败。
如果这是单元测试的方法,那么您已经完成了测试,但如果这是运行时的方法,则使用 if 条件。
Well when you use the Assert class from JUnit, any method in there are all void, but it uses the JUnit framework to tell you if the test has pass or failed.
If this is method for unit testing, then you have accomplished your test, but if this were ment for runtime, then use a if condition.
我建议您的单例使用按需初始化模式(即延迟加载单例)。在该模式中,如果实例的构造失败,您将得到一个异常,否则您将获得该对象。
这从两个方面改进了您的解决方案:它没有同步开销,也没有
Assert
开销。如果未创建单例对象,这两种方法都会引发异常 - 在您的代码中,您将从断言中获得IllegalArgumentException
。使用按需初始化,您的代码将是:
要使用它仍然是:
...除非您不再需要
Assert
也不需要Synchronized
。I recommend using the initialization-on-demand pattern (i.e. lazy loading singleton) for your singleton. In that pattern if the construction of the instance fails you'll get an exception, otherwise you'll get the object.
This improves on your solution in two ways: it doesn't have the overhead of synchronization and it doesn't have the overhead of the
Assert
. Both methods throw an exception if the singleton object is not created - in your code you'll get anIllegalArgumentException
from the assert.Using init-on-demand your code would be:
To use it would still be:
...except you no longer need neither the
Assert
nor theSynchronized
.