断言单例对象不为空

发布于 2025-01-07 02:02:28 字数 592 浏览 1 评论 0原文

是否有必要对单例对象进行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单身情人 2025-01-14 02:02:28

Assert 不是必需的,因为 LibraryFrame 实例将始终在此时初始化。

The Assert is not necessary since the LibraryFrame instance will always be initialized at this point.

夜声 2025-01-14 02:02:28

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 static getLibraryFrame() method doesn't return null, but it's not worthwhile to assert this at run-time.

蓝色星空 2025-01-14 02:02:28

当您使用 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.

豆芽 2025-01-14 02:02:28

我建议您的单例使用按需初始化模式(即延迟加载单例)。在该模式中,如果实例的构造失败,您将得到一个异常,否则您将获得该对象。

这从两个方面改进了您的解决方案:它没有同步开销,也没有 Assert 开销。如果未创建单例对象,这两种方法都会引发异常 - 在您的代码中,您将从断言中获得 IllegalArgumentException

使用按需初始化,您的代码将是:

public class ComponentFactory 
{
  private ComponentFactory() 
  {
  }

  private static class LazyHolder 
  {
    public static final LibraryFrame INSTANCE = new LibraryFrame();
  }

  public static LibraryFrame getLibraryFrame() 
  {
    return LazyHolder.INSTANCE;
  }
}

要使用它仍然是:

LibraryFrame libraryFrame = ComponentFactory.getLibraryFrame();

...除非您不再需要 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 an IllegalArgumentException from the assert.

Using init-on-demand your code would be:

public class ComponentFactory 
{
  private ComponentFactory() 
  {
  }

  private static class LazyHolder 
  {
    public static final LibraryFrame INSTANCE = new LibraryFrame();
  }

  public static LibraryFrame getLibraryFrame() 
  {
    return LazyHolder.INSTANCE;
  }
}

To use it would still be:

LibraryFrame libraryFrame = ComponentFactory.getLibraryFrame();

...except you no longer need neither the Assert nor the Synchronized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文