Loose coupling occurs when the dependent class contains a pointer only to an interface, which can then be implemented by one or many concrete classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// tight
public class ComplexAlgorithm {
bubbleSort bbl = new bubbleSort(); // it will sirectly creats the instance, and you have to make the change everything in here.
}
//loose
@Component
public class ComplexAlgorithmImpl {
@Autowired
private SortAlgorithm sortAlgorithm;
public ComplexAlgorithmImpl(SortAlgorithm algorithm) {
this.sortAlgorithm = algorithm;
}
}
public interface SortAlgorithm {
public int[] sort(int[] numbers);
}
public class QuickSortAlgorithm implements SortAlgorithm {
}
|