I want to use the mediator design pattern (http://en.wikipedia.org/wiki/Mediator_pattern) in my netbeans platform modular app. However, I am not sure 开发者_JAVA百科how to do this on startup, since there does not seem to be a good way to create my mediator and then inject it into each module.
Does anyone know a good way to do this? Do I have to resort to using the Lookup API or something?
When it comes to the NetBeans platform and inter-modular communications most answers boil down to the lookup :)
Using the sample code that you gave I'd do the following
@ServiceProvider(service = Mediator.class)
class Mediator{..}
The ServiceProvider annotation is a NetBeans extension to the ServiceLoader mechanism that automates the work of having to put values in the META-INF/services folder.
The ButtonView class would be modified as follows
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();
}
}
I'm not really familiar with the Mediator pattern, so I hope my understanding passes muster in as much as that you can understand the example.
For more examples see these sites
- NetBeans Platform QuickStart
- NetBeans Platform – how to register a class as a provider of multiple services
- NetBeans Lookups explained
- NetBeans Developer FAQ - Lookup
精彩评论