Question

I've recently seen the Netbeans platform development tutorial videos (now known as trails) (http://www.netbeans.org/kb/trails/platform.html)

and I've been wondering how stable using Lookup from Netbeans

this is what i understood about how the Lookup works

as an Image :

enter image description here

as Psudo code (class stucture):

{

((class A implements MyInterface)) , ((class B implements MyInterface))


[MyInterface (API)]

[CoreApplication] ([Lookup provide access to A and B via MyInterface ])

}

My question is that does above implementation creates a reliable outcome compared to heavily coupled (also think of a scenario where static variables,methods and inner classes are accessed from other classes) ?

[i need to know that if i use loosely coupling and use lookup api to implement it. will my program become more or less reliable compared to tightly coupled one. (by reliable i mean factors like stability of the application, will it crash often, etc)]

If so how it works ?

Was it helpful?

Solution 2

I'd say it (also) depends on you if it's reliable.

Think about it. You have class B registered in the lookup, class A needs a service that class B would be able to provide. All class A wants is the service, it doesn't matter if the implementation is actually from class B, class C, class D or whatever. So, by using the lookup you simply ask if your service is available and use it if it is. And here it comes to reliability. Let's say the service you're looking for cannot be found within the lookup, then it's up to you to make sure that the system won't break if the service cannot be found.

A scenario in NetBeans could look like this:

RequiredService s = Lookup.getDefault().lookup(RequiredService.class);
if (s != null)
{
    s.doSomething(); // In this case the responsibility of reliability lies on the side of who implemented this service.
}
else
{
    // Now it's up to you to keep it reliable and perhaps let the user know that something went not as expected.
}

Now imagen you "tightly" rely on the implementation of RequiredService and all of a sudden, for some reason, it's not there anymore. Since modules are loaded at runtime within NetBeans, you'll get those ClassLoader exceptions and won't even have the chance to make a decision what to do in such cases.

OTHER TIPS

Striving for the loose-coupling (and programming to interface rather to implementation) is one of the fundamental principles you should adhere to.

This way you can change the implementation later (when you change your mind or specifications change) without affecting the dependent code which use the to-be-changed class.

Its like you are in business and have a contractor which is bound by the contract to do certain thing with agreed upon input. When you decide that you want another contractor (doing the SAME stuff with the SAME input but for the less money or faster) you just change him without effect on your business. Because you are not tightly coupled. This principle of not being to reliant on some entity and 'connect to it' via interface only (set of predefined methods with fixed input/output) goes beyond the programming concepts and can be applied to other areas of life/business as it is a beneficial thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top