Friday, September 21, 2007

Loose coupling






[Time: 4:32 mins]


Loose (or low) coupling means (in a generic sense) that an entity can interact with another entity without being concerned with the internal implementation, and through a stable interface. Low coupling is implemented by using principles of information hiding. When two entities are loosely coupled, internal changes in one of them do not affect the other. The calling method is coupled to the called method and not it's implementation. Since we have already discussed encapsulation and information hiding in detail, I will not repeat it here.

  • Loose coupling is achieved with information hiding
  • And by programming to the interface rather than the implementation

Example 1: Tightly coupled system

public void aMethod() {

ArrayList myList = new ArrayList();

doSomethingWithList(list);

}

private void doSomethingWithList(ArrayList list) {

}


Example 2: Loosely coupled system

public void aMethod() {

List
myList = new ArrayList();

doSomethingWithList(list);

}

private void doSomethingWithList(List list) {

}

Java2html
  • Loose coupling contains the scope of change when an implementation is modified



Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

No comments: