'Java interface loose coupling advantages in realworld?
Can someone help me out, I read some Java tight and loose coupling article. I had certain doubts in loose coupling watched several YouTube videos and articles, but still couldn't grasp certain points. I will explain what I understood and what confuses me.
In loose coupling we restrict the direct coupling between classes. But in tight coupling we are bound to classes. Let's take an example. I have one Main class and another different class with the name Apple. I am creating a instance of this class in Main class by
Apple apple =new Apple();
//Apple is tightly coupled to Main class
apple.eat();
//If eat method signature is Changed to drink in the Apple class of course we need to change the method name here in Main class also right?.
Let's see loose coupling
class interface Fruits{
void drink();
}
Class Apple implements Fruits {
@Override
public void drink (){
// Printing some message;
}
}
class Main{
public static void main(String [] args){
Fruits apple = new Apple ();
//is this loose coupled
apple.drink();
}
}
If I change the method signature in loose coupling from drink to pour, I need to change the code in three different places.
- method signature inside Fruits interface (drink to pour)
- class Apple (method override from drink to pour)
- inside Main class (method call from apple.drink to apple.pour)
What's the point of using loose coupling here? In tight coupling once I modify the coupled class (Apple), I am forced to update the Main class. In loose coupling also I am doing the same process. What's the point of using it?
The only benefit that I can feel through loose coupling is the interface reference type, for example
Tight coupling code
class Main {
//boiler plate code
Apple apple = new Apple();
}
In future if I create Mango class, I need to create another object in Main class like Mango mango = new Mango();
Loose coupling code
class Main {
//boiler plate code
Fruits apple =new Apple():
//In future if i Change Apple() to Mango() no error will occur here because of the interface reference type(Fruits).
}
And what is code extensibility in interface if I introduce a new method signature, all dependents are broken and force us to implement the new method. Then how to safely extend interface without breaking existing code
Please help me to understand with this fruit analogy.
Solution 1:[1]
First of all you should check this two principles Interface Segregation Principle and Dependency Inversion Principle. It depends your problem.
Let's back your question.
I think If you create one more layer, you should understand what it is.
Let's assume that you have a Fruit Press class.
-Tight Coupled.
class FruitPress {
private Apple apple;
public FruitPress(Apple fruit){
this.apple = fruit;
}
public press() {
apple.press();
}
}
FruitPress class depends apple class. So FruitPress is tight coupled with Apple.
-Loose Coupled
class FruitPress {
private Fruit fruit;
public FruitPress(Fruit fruit){
this.fruit = fruit;
}
public press() {
fruit.press();
}
}
Your FruitPress class is loose coupled now.
And now create a Main class. If you don't use loosely coupling you must implement two presser classes(ApplePresser, OrangePresser) for your problem. But now we have a one implementation.
class Main {
public static void main(String [] args){
FruitPress applePresser = new FruitPress(new Apple());
FruitPress orangePresser = new FruitPress(new Orange());
//doing something.
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | G.Guvenal |