我想在我的 netbeans 平台模块化应用程序中使用调解器设计模式 (http://en.wikipedia.org/wiki/Mediator_pattern)。但是,我不确定如何在启动时执行此操作,因为似乎没有一种好的方法来创建我的中介然后将其注入每个模块。
有谁知道这样做的好方法?我是否必须求助于使用 Lookup API 之类的?
我想在我的 netbeans 平台模块化应用程序中使用调解器设计模式 (http://en.wikipedia.org/wiki/Mediator_pattern)。但是,我不确定如何在启动时执行此操作,因为似乎没有一种好的方法来创建我的中介然后将其注入每个模块。
有谁知道这样做的好方法?我是否必须求助于使用 Lookup API 之类的?
当谈到 NetBeans 平台和模块间通信时,大多数答案归结为查找 :)
使用您提供的示例代码,我将执行以下操作
@ServiceProvider(service = Mediator.class)
class Mediator{..}
ServiceProvider注释是对ServiceLoader 机制的NetBeans 扩展,它可以自动执行将值放入 META-INF/services 文件夹的工作。
ButtonView 类将被修改如下
class BtnView extends JButton implements Command {
Mediator med = Lookup.getDefault().lookup(Mediator.class);
BtnView(ActionListener al, Mediator m) {
super("View");
addActionListener(al);
med = m;
med.registerView(this);
}
public void execute() {
med.view();
}
}
我对中介者模式不是很熟悉,所以我希望我的理解能像你理解这个例子一样多。
有关更多示例,请参见这些站点