What is netbean's Lookup?
I have trouble understanding this. Basically, this Lookup API is used to keep loosely coupled intermodule nature. So basically a service provider and consumer modules can each communicate with each other using the Lookup API correct ?
But what I don't understand is:
is Lookup like a bag full of which objects for that Class? Can someone give an easier analogy ?
So the dependencies is created, and you implement the LookupListener in the service consumer correct ? Obviously consumer has dependency on provider.
Then what is the implementation of LookupListener listening to ? It's own Lookup ? So if there is a map of another module's class, it will be stored as an object inside Lookup of the implementation of LookupListener ?
So lookup is kind of like a bag that can store another module's classes and it's methods ?
Is this the correct process of determining a selection ?
- in the TopComponent (view) you implement the Lookup Listener, and action Listener.
- you make a new object (from the ot开发者_JS百科her module)
associateLookup(Lookups.singleton(fff));
again, confusion with this line: what isassociateLookup()
exactly doing ?result = Utilities.actionsGlobalContext().lookupResult(Browser1.class);
what is this line doing ? what is result ? does it contain the Browser1 class (from other module) ?result.addLookupListener (this)
; Why would you add listener to result ? and what are we listening for and why on the TopComponent ?Done ?
And finally, to further my confusion , how does Node API come into pla7y ?
You can think of Lookups as a basic tool that supports loose coupling high cohesion principle.
Basically you have an API in beverage-api
module:
public interface Beverage {
...
}
Then another module beers
which depends on beverage-api
:
@ServiceProvider(service = Beverage.class)
public class SomeBeer implements Beverage {
...
}
in another module which also depends on beverage-api
you can write a magic formula:
Collection list = Lookup.getDefault().lookupAll(Beverage.class);
which will get you a list of all beverage providers around without declaring exact dependency on the specific class or having dependency on that module. This is great, your code is not dependent on specific implementation, it's enough to have these modules on class path and they will "auto-magically" load into your application.
associateLookup(Lookups.singleton(fff));
again, confusion with this line: what is associateLookup() exactly doing ?
Yes, that's confusing. Basically you're manually adding some object to lookup system.
result = Utilities.actionsGlobalContext().lookupResult(Beverage.class);
Utilities.actionsGlobalContext()
is related to currently selected (active) TopCompoment
. It will return an instance of Beverage.class
if it exists in the active component. If you want all instances of given class you should use lookupAll()
.
result.addLookupListener(this);
Why would you add listener to result ?
To get notification about changes. When user selects some Beverages
objects it triggers LookupListener
method:
void resultChanged(LookupEvent ev);
and result.allInstances();
will return which instances were selected.
There is a good discussion of the NetBeans platform Lookup API.
精彩评论