Search code examples
javanetbeans-platformloose-coupling

Is loosely coupling more reliable (based on lookup API from netbeans)?


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 ?


Solution

  • 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.