Coupling Tutorial

The term coupling is commonly used to describe a device that connects one object to another. One simple example that comes to mind is a garden hose coupling. The actual coupling is the threaded piece on the end of the hose that allows you to couple the hose to a spigot. Image trying to somehow (duct tape maybe) connect a rubber hose to a spigot without the coupling ... good luck right??? In the world of Java the term coupling refers to the connection that two classes have with each other. There are two types of coupling in Java: loose-coupling and tight-coupling. If I go back to the hose coupling for minute, it is a bad thing to have a loose-coupling (water drips and sprays everywhere) and a good thing to have a tight coupling (no drips). Back in the world of Java it is the exact opposite, loose = good, tight = bad.

Tight Coupling = Bad

The term tight coupling is used to describe just how much an outside class can get its grimy little paws into another class's members. Tight coupling is synonomous with a lack of encapsulation and a poorly designed API. Consider the following code:
public class TightCoupling {
     public static void main(String args[]) {
         Box b = new Box(5,5,5);
         System.out.println(b.volume);
     }
}
public class Box {
     public int volume;
     Box(int length, int width, int height) {
         this.volume = length * width * height;
     }
}
Someone who is new to programming and just learned about constructors may very well write the exact Box class code above. In the example above, the "tight" in tight coupling means that class A has tight (direct) coupling to the instance variable volume in class Box. This sort of "tight" coupling is very bad because once the novice programmer realizes the ramifications of his mistake and changes volume to private and adds a getter method, the tight coupling between A and Box will be broken. Just to reiterate - tight coupling is synonomous with a lack of encapsulation and a poorly designed API.

Loose Coupling = Good

The term loose coupling is used to describe just how much access an outside class has to another class with a well designed API. Loose coupling is synonomous with excellent encapsulation and a well-designed API. Consider the following code:
public class LooseCoupling {
     public static void main(String args[]) {
         Box b = new Box(5,5,5);
         System.out.println(b.getVolume());
     }
}
public final class Box {
     private int volume;
     Box(int length, int width, int height) {
         this.volume = length * width * height;
     }
     public int getVolume() { return volume; }
}
In the example above, the Box class is just beginning to take shape. The object state (private int volume) cannot be coupled directly by an outside class. We have a constructor that calculates the volume when the object is created. The class is final so it cannot be inherited. We have a single getter (accessor) method that returns the value of volume. At this stage the Box class is well encapsulated and has a very simple API.
The Box class is well poised for future expansion ... we can add setter methods, we can add a private method to caculate the volume outside of the constructor, we can add more constructors, we can remove the final modifier if we decide that we want to allow inheritance, add a public static method to calculate the volume without even using the private int volume, etc.
The whole point is that we can control the various different ways that an outside class can couple with our Box class. As long as the outside class cannot get "inside" and tightly couple with our Box object state, then the relationship is loosely-coupled.




Final thoughts

None


Tutorials