Q What is the difference between Loose Coupling and Tight Coupling?
Ans Java supports 2 types of coupling between classes
Rack.java
a
Ans Java supports 2 types of coupling between classes
- Loose Coupling
- Classes are Independent of each other.
- Class A has only that knowledge that is exposed via its interface
- For example Let us take a scenario from Library Management System of adding Books to Racks.
- In the below Class "Book" we have instantiated RackA class and RackB class.
- RackB class is a replica of RackA class with just change in name so we have not added it here.
- Now we see that our instance is Type of interface i.e. Rack so only methods and Properties of Rack is visible to Book and nothing else.
- So any changes made in class RackA or RackB will not be a concern for Book.
- Using this Interface we can inject any of the implemented classes at runtime and provide service to End User.
- Also in Rack A we see we have a private variable "Shelf".
- This is also exposed via "getShelf()" method.
- Now if we even divide our Shelf into two in future for "RackA".We might have an sections in shelf.
- In such a case we will only change our "getShelf()" method and will get desired output.We will not be concerned with any of the Books related to that Shelf.
- The motto of story is the more you expose the more tight coupling you will have which is not good in most of the cases.
- This is called as Loose Coupling between the objects.
 /*  
  * To change this license header, choose License Headers in Project Properties.  
  * To change this template file, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 package LooseCoupling;  
 /**  
  *  
  * @author Gaurav Matta  
  */  
 public class Book {  
   public static void main(String[] args)  
   {  
     System.out.println("In Main");  
     Rack r1=new RackA();  
     Rack r2=new RackB();  
     r1.setShelf(1);  
     r1.addBooks("Introduction to Java");  
     r1.addBooks("Android Basics");  
     r1.setShelf(2);  
     r1.addBooks("Introduction to J2EE");  
     r1.addBooks("Advanced Java Concepts");  
     System.out.println("Books in Shelf 1");  
     r1.getBooksByShelf(1);  
     System.out.println("Books in Rack");  
     r1.getBooksInRack();  
     r2.setShelf(1);  
     r2.addBooks("Introduction to Java");  
     r2.addBooks("Android Basics");  
     r2.setShelf(2);  
     r2.addBooks("Introduction to J2EE");  
     r2.addBooks("Advanced Java Concepts");  
     System.out.println("Books in Shelf 1");  
     r2.getBooksByShelf(1);  
     System.out.println("Books in Rack");  
     r2.getBooksInRack();  
   }  
 }  
 /*  
  * To change this license header, choose License Headers in Project Properties.  
  * To change this template file, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 package LooseCoupling;  
 /**  
  *  
  * @author Gaurav Matta  
  */  
 public interface Rack {  
   public int shelves=5;  
   void getBooksByShelf(int shelf);  
   void getBooksInRack();  
   void addBooks(String book);  
   void setShelf(int shelf);  
 }  
RackA.java
 /*  
  * To change this license header, choose License Headers in Project Properties.  
  * To change this template file, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 package LooseCoupling;  
 import java.util.ArrayList;  
 /**  
  *  
  * @author Gaurav Matta  
  */  
 public class RackA implements Rack {  
   private int shelf;  
   private final ArrayList<String> books[]=new ArrayList[shelves];  
   public int getShelf() {  
     return shelf;  
   }  
   @Override  
   public void setShelf(int shelf) {  
     this.shelf = shelf;  
   }  
   @Override  
   public void addBooks(String book) {  
     if(this.books[shelf]==null)  
     {  
       this.books[shelf]=new ArrayList();  
     }  
     this.books[shelf].add(book);  
   }  
   @Override  
   public void getBooksByShelf(int shelf)  
   {  
      System.out.println(this.books[shelf]);  
   }  
   @Override  
   public void getBooksInRack()   
   {  
     for(ArrayList s:books)  
     {  
       System.out.println(s);  
     }  
   }  
 }  
- Tight Coupling
- Tight coupling implies two classes change together.
- Class A has more than required access to Class B.
- Any changes in class Rack will lead to change in Class Book.
- If we change the way how shelves are divided in sections in future then we will have to change in each type of Book Class.
- This is not recommended.
 /*  
  * To change this license header, choose License Headers in Project Properties.  
  * To change this template file, choose Tools | Templates  
  * and open the template in the editor.  
  */  
 package TightCoupling;  
 import java.util.ArrayList;  
 /**  
  *  
  * @author Gaurav Matta  
  */  
 public class Book {  
   public static void main(String args[])  
   {  
     Rack r1=new Rack();  
     Rack r2=new Rack();  
     int shelf=2;  
     if(r1.books[shelf]==null)  
     {  
       r1.books[shelf]=new ArrayList();  
     }  
     r1.books[shelf].add("Introduction to Java");  
     r1.books[shelf].add("Thumbs Up Java");  
     shelf=1;  
     if(r2.books[shelf]==null)  
     {  
       r2.books[shelf]=new ArrayList();  
     }  
     r2.books[shelf].add("Java Needs");  
     r2.books[shelf].add("Java Stream");  
     r1.getBooksInRack();  
     r2.getBooksInRack();  
   }  
 }  Rack.java
 public class Rack   
 {  
   public int shelf;  
   public final ArrayList<String> books[]=new ArrayList[shelves];  
   public void getBooksByShelf(int shelf)  
   {  
      System.out.println(this.books[shelf]);  
   }  
   public void getBooksInRack()   
   {  
     for(ArrayList s:books)  
     {  
       System.out.println(s);  
     }  
   }  
 }  
a
 
No comments:
Post a Comment