« Return to Thread: Best way to share object instances between modules?

Re: Best way to share object instances between modules?

by Wade Chandler :: Rate this Message:

Reply to Author | View in Thread

--- Sergio Lopes <knitter.is@...> wrote:

> >      IncomeTaxCalculator calc =
> >  Lookup.getDefault().lookup(IncomeTaxCalculator.class);
> >
> >  Note that you do not ever need to directly instantiate any type of
> >  IncomeTaxCalculator.  No code, aside from that in Module 2, should
> >  even know the name of the IncomeTaxCalculator implementation class --
> >  everything should use the interface.
> The way you're describing it implies that there has to be a module
> with an interface and another with the implementation. I have no such
> thing.
>
> What I have is something in the lines of:
> - Module Catalog provides the application logic, it exposes, through
> public methods, all available actions.
> - Module GUI provides all interface options, top components, dialogs,
> actions, and so on. It must access the CatalogEngine class, inside the
> module Catalog, in order to send user input and request all the data
> to display.
> - Module GUI depends on Module Catalog, that in turn depends on some
> other modules that wrap various libraries I need.
>
> I have tried to look for an instance of the class CatalogEngine, the
> lives inside the Catalog module, in one of my TopComponent's subclass,
> that lives inside the GUI module. I tried that because I read
> somewhere, either in the wiki or the API docs, that I could pass an
> Interface or a Class, but I always get null.
> I have tried to get the instance in various places, an installer class
> on the GUI module, various constructors for classes inside the GUI
> module. The result is always null.
>

It sounds to me like you have a misunderstanding of the way lookup works. I might be wrong, and
maybe you are not trying to use Lookup to find an instance of CatalogEngine, but it  sounds to me
you are expecting CatalogEngine to be mystically put into the Lookup.

Some how you have to make your module put this class in the Lookup. You can do this different
ways, but a couple of workable solutions:

1) use InstanceContent as the data for your Lookup and either expose it directly to your modules
classes which will be manipulating or hide it behind some methods.

2) use the layer file system and then use Lookups.forPath. See:
http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/doc-files/api.html#folderlookup

for more information, but it works well, and you can get really flexible with this as well.
Notice what happens if those instances are assignable to Lookup...they are also proxied in Lookup
calls. So, you can get real powerful there building your own layer API for looking up content and
getting it into lookups.

Anyways, I'm inferring that you are having problems centered around not being able to get your
instance into the lookups you are looking for them in, and this is how. If I'm wrong, then sorry
for misunderstanding.

You have to provide the lookup which you are accessing. In the case of option 2 above you aren't
necessarily providing Lookup as much as you are asking for one to be created from a hierarchy on
your behalf which may actually contain other Lookup instances which can also be proxied in your
calls.

Wade


==================
Wade Chandler, CCE
Software Engineer and Developer, Certified Forensic Computer Examiner, NetBeans Dream Team Member, and NetBeans Board Member
http://www.certified-computer-examiner.com
http://wiki.netbeans.org/wiki/view/NetBeansDreamTeam
http://www.netbeans.org

 « Return to Thread: Best way to share object instances between modules?

LightInTheBox - Buy quality products at wholesale price