2010-03-26 50 views
0

我有BeanTreeView和其中的一些节点。每个节点都有构造器查找工作如何?

public class ProjectNode extends AbstractNode { 

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

} 

我ExplorerTopComponent设置根节点为根的树,因为这:

private final ExplorerManager mgr = new ExplorerManager(); 

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

而现在,我怎么可以从一些节点获得MainProject OBJ?我需要在另一个班上学习。

回答

1

Hi badgirl/Joseph ;-)

要回答你的问题的标题(有关NetBeans内部没有):

我觉得this question回答您的问题what the lookup is一点点:检索一个或多个实施提供通常是界面的键。然后约瑟夫问道:'哪种简单的图案可以代替这种反面图案?'。 您无法替换NetBeans平台中的查找模式,但要回答他的问题:我将使用dependency injection而不是通过手动接线,guice,pico容器甚至弹簧在我的应用程序中查找。 关于这样一个库的好处在于,您只需配置依赖关系并从容器中检索正确配置的实例。看到this nice picture得到这个感觉。 (最依赖注入工具还允许生命周期管理)。

要给出了通过手工布线依赖注入的例子假设下面的类:

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

现在您可以轻松切换的ArrayList实现了链表:

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

与查找的概念,这是不容易的(但我不知道):

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

现在用链表更换的使用是直截了当:

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

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

你这取决于使用它的类实现您的查找:

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

但说实话:我更喜欢简单的依赖注入方式。阅读this introduction,他们还在这里比较查找和依赖注入(构造函数+ setter注入)。

欲了解更多信息,请阅读this interesting article from martin fowler描述服务定位器(查找)和控制反转(依赖注入)。在这里阅读有关history of dependency injection