查找如何工作? [英] How does lookup work?

查看:93
本文介绍了查找如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有BeanTreeView以及其中的一些节点.每个节点都有构造函数

I have BeanTreeView, and some nodes in it. Every node has constructor

public class ProjectNode extends AbstractNode {

public ProjectNode(MainProject obj, DiagramsChildren childrens) {
    super (new ProjectsChildren(), Lookups.singleton(obj));
    setDisplayName ( obj.getName());

}

我将Rootnode设置为ExplorerTopComponent中树的根:

I set Rootnode as a root for tree in ExplorerTopComponent as this:

private final ExplorerManager mgr = new ExplorerManager();

public ExplorerTopComponent() {
    associateLookup (ExplorerUtils.createLookup(mgr, getActionMap()));
    mgr.setRootContext(new RootNode());
}

现在,如何从某个节点获取MainProject obj?我需要在另一堂课上得到它.

And now, how I can get MainProject obj from some node? I need to get it in another class.

推荐答案

嗨badgirl/Joseph ;-)

要回答您的问题的标题(与netbeans内部无关):

To answer the title of your question (nothing about netbeans internals):

我认为此问题回答了您的问题查找内容:检索提供的键的一个或多个实现,该键通常是一个接口.

I think this question answers a little bit of your question what the lookup is: retrieving one or more implementation to the provided key which is normally an interface.

然后约瑟夫问:哪个简单模式可以代替此反模式?". 您不能在NetBeans平台中替换查找模式,而是要回答他的问题:我将使用依赖注入,而不是通过手工接线,guice,微微容器甚至弹簧在我的应用程序中查找. 这样的库的好处是,您只需配置依赖项,然后从容器中检索正确配置的实例.请参阅这张漂亮的照片以了解一下. (大多数依赖项注入工具还允许生命周期管理.)

Then Joseph asked: 'which simple pattern can replace this anti pattern?'. You can't replace the lookup pattern in the NetBeans Platform but to answer his question: I would use dependency injection instead of lookup in my app via hand wiring, guice, pico container or even spring. The nice thing about such a library is that you configure just the dependencies and retrieve properly configured instances from the container. See this nice picture to get a feeling for this. (The most dependency injection tools allow also lifecycle management.)

举一个通过手工接线进行依赖注入的例子,假设下面的类:

To give an example for dependency injection via hand-wiring assume the following class:

class Path {
    List edges = new ArrayList();
    public void setEdges(List e) {
     edges = e;
    }
}

现在,您可以使用LinkedList轻松切换ArrayList实现:

Now you can easily switch the ArrayList implementation with a LinkedList:

p = new Path();
p.setEdges(new LinkedList());

使用查找概念,这并不容易(但我不确定):

With the lookup concept this is not as easy (but I am not sure):

class Path {
  List edges = Lookup.create(List.class);
}

现在,替换链接列表的用法很简单:

Now the usage with the linked list replacement is straight forward:

Lookup.set(List.class, LinkedList.class);
p = new Path();    

但是问题是:如果您有几个需要List实现的不同类,您将如何使用查找?

but the problem is: How would you use lookup if you have several different classes which needs a List implementation?

您可以根据使用它的类来实现查找:

you implement your lookup depending on the class which uses it:

class Path {
  List edges = Lookup.get(Path.class).create(List.class);
}

但是说实话:我更喜欢简单的依赖注入方式.阅读此介绍,他们还比较了查找和依赖项注入(构造函数+ setter注入) ).

But to be honest: I prefer the simple dependency injection way. Read this introduction where they also compared lookup and dependency injection (constructor + setter injection).

有关更多信息,请阅读这篇来自马丁·福勒的有趣文章,其中描述了服务定位器(查找)和控制反转(依赖注入).在这里阅读有关依赖项注入的历史.

For more information read this interesting article from martin fowler which describes service locators (lookup) and inversion of control (dependency injection). Read here about the history of dependency injection.

这篇关于查找如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆