Header Ads

ad

Bean Collaboration | Tight Coupling | Loose Coupling

>> Keeping spring bean classes in dependency/association is called bean collaboration.

Target (Main class)----->Dependent(Helper class)
Flipkart------>DTDC
Vehicle------> Engine

>> In real Project Development we can't develop entire logic in one class. We need to take multiple classes and these classes will maintain dependency with each other.

>> The class that uses other class logics/methods is called target class and the class that act as helper class is called dependent class and we can say both classes are in collaboration.

>> For example Flipkart and DTDC classes must be in collaboration. i.e Flipkart is target class and DTDC is dependent class.

>> To make target/main class using logics of dependent class we can use the following approaches.

    Different Approaches of Keeping Java classes in collaboration/ in dependency

    1) Create dependent class object in target class. This is also know as HAS-A relation or composition.
    2) Target class extends from dependent class. This also known as Inheritance.
    3) Make underlying container(IOC container) inject dependent class object to target class(Setter/Constructor injection).

    Approach 1: Create Dependent class object in target class .

    Target class
    --------------
    public class Flipkart{

       DTDC dtdc=new DTDC();
      
       public void shopping(String items[])
       {
           //shopping logic
            ------------
            ------------
           dtdc.deliver();
       }

    }

    Dependent class
    ------------------
    public class DTDC{
      
       public void deliver(){

         ------
         ------
      }
    }

    Limitations :-
    ---------------
    If we did not like DTDC and want to use the services of other dependent classes such as BlueDart or FirstFlight then in Flipkart class we need to change the source code from DTDC to BlueDart or FirstFlight. This shows tight coupling, so this approach is not good.


    Approach 2: Make target class extends from dependent class .

    Dependent class
    ------------------
    public class DTDC{
      
       public void deliver(){

         ------
         ------
      }
    }

    Target class
    --------------
    public class Flipkart extends DTDC{
      
       public void shopping(String items[])
       {
           //shopping logic
            ------------
            ------------
           deliver();
       }
    }


    Limitations
    ---------------
    a) Flipkart already extends DTDC. So, it can not extends more classes.
    b) In order to change dependency from DTDC to BlueDart we need to make Flipkart extends from BlueDart, to do it we need to modify the source code of Flipkart, this shows tight coupling.
    c) So, this approach is not good.



    Approach 3: Make underlying container injecting dependent class object to target class using setter or constructor injection

    Dependent class
    ---------------------
    public class DTDC{
      
       public void deliver(){

         ------
         ------
      }
    }

    Target class
    ------------------
    public class Flipkart{
      
       private DTDC dtdc;
       
       public void setDtdc(DTDC dtdc)
       {
          this.dtdc=dtdc;
        }

       public void shopping(String items[])
       {
           //shopping logic
            ------------
            ------------
           dtdc.deliver();
       }
    }


    applicationContext.xml
    -------------------------------

    <beans--->

        <bean id="dtdc" class="pkg.DTDC"/>

        <bean id="fpkt" class="pkg.Flipkart">
            <property name="dtdc" ref="dtdc"/>
        </bean>

    </beans>


    Pros
    ----------
    Here IOC container takes care of bean management and dependency management .

    Cons
    -----------
    This approach is good because bean management is taken care by IOC container but It also shows tight coupling, to move from DTDC to BlueDart we need to modify the source code of Flipkart class.

    So, to achieve loose coupling in Spring, use "POJI Model Programming+Approach-3" shown below.


    POJI Model Programing

    >> It is recommended to make all possible dependent classes implement common interface having common method declaration. So we can use this common interface reference variable in target class.

    >> The above Flipkart class is not working with implementation class names directly, it is working with common interface name "Courier", so we can make IOC container inject any implementation class object to Flipkart(target class).

    >> The above code shows loose coupling between target class(Flipkart) and dependent classes.










    No comments