Understanding Coupling and How to Achieve Low Coupling in Object‑Oriented Design
This article explains the concept of coupling in object‑oriented programming, distinguishes strong and weak coupling with Java examples, and presents two practical techniques—upcasting/interface callbacks and the adapter pattern—to achieve low coupling and improve code modularity.
In software development we often emphasize low coupling and high cohesion, but what exactly is coupling and how can we achieve low coupling? This article explains the concept of coupling, distinguishes strong and weak coupling, and presents two main ways to decouple classes.
What is Coupling?
Coupling refers to the degree of direct dependence between two classes. The following example shows strong coupling where class B ’s method requires an instance of class A .
public class A{
public int i;
}
public class B{
public void put(A a){
System.out.println(a.i);
}
}The next example demonstrates weak coupling using an interface, allowing class C to work with either A or B .
public interface IBase{
void say();
}
public class A implements IBase{
@Override
public void say(){
System.out.println("I am A");
}
}
public class B implements IBase{
@Override
public void say(){
System.out.println("I am B");
}
}
public class C{
public void put(IBase base){
base.say();
}
}How to Achieve Low Coupling?
Low coupling is achieved by breaking direct dependencies and introducing indirect relationships. Two common approaches are presented.
1. Using Upcasting or Interface Callbacks
Both approaches rely on polymorphism. The following code shows upcasting to a base class to decouple.
public class Base{
public void say(){
System.out.println("I am Base");
}
}
public class A extends Base{
@Override
public void say(){
System.out.println("I am A");
}
}
public class B extends Base{
@Override
public void say(){
System.out.println("I am B");
}
}
public class C{
public void put(Base base){
base.say();
}
}2. Using the Adapter Pattern
By inserting an adapter layer, direct relationships become indirect, making the system more flexible. The example below illustrates this pattern.
public class A{
public void aSay(){
System.out.println("I am A");
}
}
public class Base{
public A a;
public Base(A a){
this.a = a;
}
public void baseSay(){
a.aSay();
}
}
public class B{
public void put(Base base){
base.baseSay();
}
}
public static void main(String[] args){
A a = new A();
Base base = new Base(a);
B b = new B();
b.put(base);
}The essence of decoupling is to replace direct class relationships with indirect ones, whether through inheritance, interfaces, or adapter patterns, thereby turning strong coupling into weak coupling.
These are personal insights; feedback is welcome.
Author: 忽如一夜听春雨 Link: blog.csdn.net/qq_24499615/article/details/77821896
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.