Sunday, July 27, 2008

Strongly Coupled Vs Loosely Coupled Classes in Java

Flexibility Vs Readability in J2EE :

(‘Flexibility’ of the application is inversely proportional to ‘Simplicity’ or ‘Readability’)

It is a common problem in Java /J2EE, when the application is designed in a more flexible way. When designing an application as a generalized way, the code becomes more complicated to read and understand. But conversely, such a code is easy to extend the application’s additional functionality.

But then, when the application’s functionality is added distinctly for every functionality simply, the code completion is easy and gets completed soon as expected, but this brings-in some other overhead. Application code becomes very large and maintenance of such application becomes very high and it may need re-deployment for every minor changes. When the similar functionality is added, you may still need to devise a new mechanism and new coding efforts.


Loosely Coupled and Strongly Coupled Classes in Java / J2EE

Strongly Coupled classes:

Strongly coupled classes have very-high dependancy. For example, consider the following code:

public int findSize(ArrayList myList)
{
int s = myList.size();
}


The above code is simple to write but brings-in quite a few complications during the run-time, which may be very difficult. This is called strongly coupled class. Because this enforces a lot of conditions for example, myList should not be NULL or myList cannot be any other type other than ArrayList, etc.,


Loosely Coupled classes:

Loosely coupled classes are generic classes and they are used for many purposes and it is normally error-free. For example, consider the following code (only a sample scenario):

public int findSize(Object obj) throws Exception
{

if (obj == null) throw new Exception("The Object is NULL");
else if (obj instanceOf ArrayList) return ((ArrayList)obj).size();
else if (obj instanceOf Vector) return ((Vector)obj).size();
else if (obj instanceOf String) return ((String)obj).length();
else throw new Exception(“Incompatible Type”);
}


In the above method, the calling method can pass any type of parameters and this can be even more generalized to be useful for many types. Such classes are called loosely coupled classes.

Reflection Pattern:

Java’s reflection pattern is the most-beautiful example and you can even develop many frameworks based on this.

Hope this concept is useful. I would meet you tomorrow with yet another real-time scenario in the next blog. Till then, take care. Bye.

Regards,
Swathika.
Write to me your queries to swathikalakshmi@gmail.com

5 comments:

aviral said...

Why so many else if when u returning in every if condition ..wont it be simply if(o instanceOf String) return (String)o.length and somethin like that

Nagaraju Ganapavaram said...


Hai friend

I friend i want one project on struts,Ejb........


I want it urgently....plz help me

raghu said...

Hey i have doubt.

Shyamala Vydyam said...

hey most of the concepts what ever u have posted really sturdy n awesome explanations.....

M working with the internationalization ...Is there is any way converting messages on fly depending on the country ???

LOKESHKUMAR PUTTA said...

Hi

Is struts2 also having same token concept or not? If is there please explain in Detail