这段代码是什么意思?

发布于 2024-08-26 07:05:36 字数 269 浏览 4 评论 0原文

我不知道下面代码中的代码lookups.singleton的功能是什么

public class ProjectNode extends AbstractNode {

public ProjectNode(MainProject obj, ProjectsChildren children) {
    super (children, Lookups.singleton(obj));
    setDisplayName ( obj.getName());
}
}

I do not know, what is function of code lookups.singleton in code below

public class ProjectNode extends AbstractNode {

public ProjectNode(MainProject obj, ProjectsChildren children) {
    super (children, Lookups.singleton(obj));
    setDisplayName ( obj.getName());
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

故事未完 2024-09-02 07:05:36

您可以阅读有关 NetBeans 平台的 Lookup api< /a> 以获取设计模式的概述。您还可以 阅读关于名为 Lookups 的类,了解有关其方法的详细信息。

基本上,此 API 创建一个仅包含单个对象的查找对象。当您对此对象调用查找时,如果它实现/扩展了查询中使用的对象,则只会返回用于初始化该对象的对象。

Lookup 模式是 NetBeans 平台的一个非常重要的部分。

You can read about the NetBeans Platform's Lookup apis to get an overview of the design pattern. You can also read about the class named Lookups, for details about its methods.

Basically, this API creates a lookup object that only contains a single object. When the you call lookup on this object will only return the object that was used to initialize the object, if it implements/extends the object that is used in the query.

The Lookup pattern is a very important part of the NetBeans platform.

飘逸的'云 2024-09-02 07:05:36

Lookups 通常被称为服务定位器,现在通常被视为反模式,但在 5-8 年前相当常见。 singleton() 方法是类上的公共静态方法,用于本质上查找对基本全局对象的引用。想象一下它看起来像:

public class Lookups {
  public static SomeObject singleton(InputObject obj) {
    // use the parameter to return some other object
  }
}

它被视为反模式的原因是它会使单元测试或模拟代码部分变得极其困难。在 Java DI(“依赖注入”)中,像 Spring 这样的框架往往比这种方法更受青睐。

Lookups is what is generally called a Service Locator and it's generally viewed as an anti-pattern these days but was quite common 5-8 years ago. The singleton() method is a public static method on the class that is used to essentially find a reference to a basically global object. Imagine it looks like:

public class Lookups {
  public static SomeObject singleton(InputObject obj) {
    // use the parameter to return some other object
  }
}

The reason it's viewed as an anti-pattern is that it can make it extremely difficult to unit test or mock sections of your code. In Java DI ("dependency injection") frameworks like Spring tend to be favoured over this approach.

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