这段代码是什么意思?
我不知道下面代码中的代码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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以阅读有关 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.
Lookups
通常被称为服务定位器,现在通常被视为反模式,但在 5-8 年前相当常见。singleton()
方法是类上的公共静态方法,用于本质上查找对基本全局对象的引用。想象一下它看起来像:它被视为反模式的原因是它会使单元测试或模拟代码部分变得极其困难。在 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. Thesingleton()
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: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.