Saturday 24 October 2015

Difference between Loose Coupling and Tight Coupling in Java With Examples.

       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.

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 code the Journey class is dependents on Car class to provide service to the end user(main class to call this Journey class).
         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

32 comments:

  1. Thank you for this post, we are new to the blog world
    and 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.

    ReplyDelete
  2. simple and clearly understandable.thanks

    ReplyDelete
  3. I got cleared doubts....thanks for sharing.

    ReplyDelete
  4. Thank you. I understood it now.

    ReplyDelete
  5. Loose Coupling:
    public class Journey {
    Vehicle v;
    public Journey(Vehicle v){
    this.v = v;
    }
    public void startJourney() {
    v.start();
    }
    }

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. Thank you to all..If have any doubts, please send email to anilnivargi49@gmail.com

    ReplyDelete
  8. Thanks for sharing such a nice info with us. Fantastic writing skills. I appreciate this blog

    ReplyDelete
  9. Thanks for this great information. It is really useful.

    ReplyDelete
  10. It's highly changeable. which kissanime is real One module doesn't break other modules in unpredictable ways.

    ReplyDelete
  11. Thanks for sharing such a great and informative post.

    ReplyDelete
  12. I got cleared doubts....thanks for sharing.
    watch overlord season 2

    ReplyDelete
  13. here is anime movie may help you rokudenashi majutsu koushi to akashic records

    ReplyDelete
  14. Your article is such an informative article. It is glad to read such those articles. Thanks for sharing.

    ReplyDelete
  15. Thanks for more clear explaination.

    ReplyDelete
  16. Your article is such an informative article audiobook torrents

    ReplyDelete