First to understand what is Loose coupling and Tight coupling and also to know the advantage of loose coupling.
In short Loose coupling means reducing dependencies of a class that use different class directly . Tight coupling means classes and objects are dependent on one another. In general tight coupling is usually not good because it reduces flexibility and re-usability of code and it makes changes much more difficult and not easy to test.
Tightly coupled object is an object that needs to know quite a bit about other objects and are usually highly dependent on each other's interfaces. Changing one object in a tightly coupled application often requires changes to a number of other objects. In a small application we can easily identify the changes and there is less chance to miss anything. But in large applications these inter-dependencies are not always known by every programmer and there is chance of overlooking changes.(Source- from StackOverflow).
See below code is for tight coupling.
In the above code the Journey class is dependents on Car class to provide service to the end user(main class to call this Journey class).
The best example of tight coupling is RMI(Remote Method Invocation)(Now a days every where using web services and SOAP instead of using RMI, it has some disadvantages).
Loose coupling is a design goal that seeks to reduce the inter-dependencies between components of a system with the goal of reducing the risk that changes in one component will require changes in any other component. Loose coupling is much more generic concept intended to increase the flexibility of system, make it more maintainable and makes the entire framework more stable.
Below code is an example of loose coupling,
In the above example, Journey and Car objects are loosely coupled. It means Vehicle is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.
The examples of Loose coupling are Interface, JMS, Spring IOC(Dependency Injection, it can reduce the tight coupling).
A loosely coupled will help you when your application need to change or grow. If you design with loosely coupled architecture, only a few parts of the application should be affected when requirements change. With too a tight coupled architecture, many parts will need to change and it will be difficult to identify exactly which parts will be affected. In short,
1) It improves the testability.
2) It helps you follow the GOF principle of program to interfaces, not implementations.
3) The benefit is that it's much easier to swap other pieces of code/modules/objects/components when the pieces are not dependent on one another.
4) It's highly changeable. One module doesn't break other modules in unpredictable ways.
Related Post:-
1) Spring Annotations
2) Spring MVC with Hibernate CRUD Example
3) Spring MVC workflow with example
4) Factory Design Pattern in Java
5) String Interview Questions and Answers
6) What is IOC Container in Spring? Difference between BeanFactory and ApplicationContext
7) Java Date and Calendar Examples
8) Exception Handling Interview questions and answers
9) Java Program to Count Occurrence of Word in a Sentence
In short Loose coupling means reducing dependencies of a class that use different class directly . Tight coupling means classes and objects are dependent on one another. In general tight coupling is usually not good because it reduces flexibility and re-usability of code and it makes changes much more difficult and not easy to test.
Tight Coupling:-
Tightly coupled object is an object that needs to know quite a bit about other objects and are usually highly dependent on each other's interfaces. Changing one object in a tightly coupled application often requires changes to a number of other objects. In a small application we can easily identify the changes and there is less chance to miss anything. But in large applications these inter-dependencies are not always known by every programmer and there is chance of overlooking changes.(Source- from StackOverflow).
See below code is for tight coupling.
public class Journey { Car car = new Car(); public void startJourney() { car.travel(); } } public class Car { public void travel () { System.out.println("Travel by Car"); } }
In the above case Journey class is tightly coupled with Car class it means if any change in the Car class requires Journey class to change. For example if Car class travel() method change to journey() method then you have to change the startJourney() method will call journey() method instead of calling travel() method.
See below code,
public class Journey { Car car = new Car(); public void startJourney() { car.journey(); } } public class Car { public void journey () { System.out.println("Travel by Car"); } }
The best example of tight coupling is RMI(Remote Method Invocation)(Now a days every where using web services and SOAP instead of using RMI, it has some disadvantages).
Loose Coupling:-
Loose coupling is a design goal that seeks to reduce the inter-dependencies between components of a system with the goal of reducing the risk that changes in one component will require changes in any other component. Loose coupling is much more generic concept intended to increase the flexibility of system, make it more maintainable and makes the entire framework more stable.
Below code is an example of loose coupling,
public interface Vehicle { void start(); } public class Car implements Vehicle { @Override public void start() { System.out.println("Travel by Car"); } } public class Bike implements Vehicle { @Override public void start() { System.out.println("Travel by Bike"); } } // create main class Journey public class Journey { public static void main(String[] args) { Vehicle v = new Car(); v.start(); } }
In the above example, Journey and Car objects are loosely coupled. It means Vehicle is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.
The examples of Loose coupling are Interface, JMS, Spring IOC(Dependency Injection, it can reduce the tight coupling).
Advantages Of Loose coupling:-
A loosely coupled will help you when your application need to change or grow. If you design with loosely coupled architecture, only a few parts of the application should be affected when requirements change. With too a tight coupled architecture, many parts will need to change and it will be difficult to identify exactly which parts will be affected. In short,
1) It improves the testability.
2) It helps you follow the GOF principle of program to interfaces, not implementations.
3) The benefit is that it's much easier to swap other pieces of code/modules/objects/components when the pieces are not dependent on one another.
4) It's highly changeable. One module doesn't break other modules in unpredictable ways.
Related Post:-
1) Spring Annotations
2) Spring MVC with Hibernate CRUD Example
3) Spring MVC workflow with example
4) Factory Design Pattern in Java
5) String Interview Questions and Answers
6) What is IOC Container in Spring? Difference between BeanFactory and ApplicationContext
7) Java Date and Calendar Examples
8) Exception Handling Interview questions and answers
9) Java Program to Count Occurrence of Word in a Sentence
Good explanation.
ReplyDeleteGood Article
ReplyDeleteGood one.
ReplyDeleteThank you for this post, we are new to the blog world
ReplyDeleteand trying to understand what attributes we want to put on our own blog and which to
keep out. This list will sure help us to do the best for our own business.
please
ReplyDeleteeasy to understand
ReplyDeletesimple and clearly understandable.thanks
ReplyDeleteI got cleared doubts....thanks for sharing.
ReplyDeleteThank you. I understood it now.
ReplyDelete!!!!!!!!!
ReplyDeleteLoose Coupling:
ReplyDeletepublic class Journey {
Vehicle v;
public Journey(Vehicle v){
this.v = v;
}
public void startJourney() {
v.start();
}
}
This comment has been removed by a blog administrator.
ReplyDeletenallo
ReplyDeletethanks
ReplyDeleteThank you to all..If have any doubts, please send email to anilnivargi49@gmail.com
ReplyDeleteThank you ...
ReplyDeleteThanks for sharing such a nice info with us. Fantastic writing skills. I appreciate this blog
ReplyDeleteThanks for this great information. It is really useful.
ReplyDeletegood explanation of java read more
ReplyDeleteIt's highly changeable. which kissanime is real One module doesn't break other modules in unpredictable ways.
ReplyDeleteThanks for this great watch prison school season 2 information. It is really useful.
ReplyDeleteThanks for sharing such a great and informative post.
ReplyDeleteI got cleared doubts....thanks for sharing.
ReplyDeletewatch overlord season 2
nice blog must watch its anime movie Dragon ball super episode 109
ReplyDeletehere is anime movie may help you rokudenashi majutsu koushi to akashic records
ReplyDeletehere is anime movie may help you rokudenashi majutsu koushi to akashic records
ReplyDeleteYour article is such an informative article. It is glad to read such those articles. Thanks for sharing.
ReplyDeleteThanks for more clear explaination.
ReplyDeleteYour article is such an informative article audiobook torrents
ReplyDelete