Tuesday, 18 February 2014

Java Loosely and tightly coupled code or Spring Dependency Injection

Following post will explain the java tightly and loosely coupled code.And  How to avoid the tightly coupled code By using Spring Dependency Injection 

Following link have war file example. play it

Download Dependence Injection Example (War)

This is a web project war import this war to your eclipse and  right click on Employee class run the java
To view change the id ref to oracle to view change

Tightly Coupled Code:

If the one java class  completely  using the other java class's logic   means collaboration.

Example:

class SunMicrosystems {
 public void showCompanyName() {
  System.out.println("SunMicrosystems");

 }
}


/**
Employee  calls the  SunMicrosystems's   logic . If we  change the logic.we must change the java file also.
Employee class always depends the SunMicrosystems is called tightly coupled
*/


class Employee {
 SunMicrosystems sun = new SunMicrosystems();

 public void workCompany() {
  sun.showCompanyName();
 }

 public static void main(String[] arg) {
  Employee emp = new Employee();
  emp.workCompany();

 }

}

Let We See How to avoid tightly coupled code by Spring Dependency Injection.

By Introduce the Interface  we can avoid the tightly coupled code

Loosely Coupled Code: 

package com;

interface Company {
 public void showCompanyName();

}



package com;

class Oracle implements Company {
 public void showCompanyName() {
  System.out.println("Oracle");

 }
}


package com;

class SunMicrosystems implements Company {
 public void showCompanyName() {
  System.out.println("SunMicrosystems");

 }
}


package com;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

class Employee {

 private Company c;

 public void setC(Company c) {
  this.c = c;
 }

 public void showCompany() {
  c.showCompanyName();
 }

 public static void main(String[] arg) {
  Resource r = new ClassPathResource("applicationContext.xml");
  BeanFactory factory = new XmlBeanFactory(r);

  Employee emp = (Employee) factory.getBean("emp");
  emp.showCompany();
 }

}

/**
In the employee class class the method showCompany(), It in company  Interface , Following xml file we can change the ref attribute itself can desired ,the method call from which  class
*/


<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="sun" class="com.SunMicrosystems" />
<bean id="oracle" class="com.Oracle" />
  <bean id="emp" class="com.Employee">
    <property name="c" ref="sun" />
  </bean>

</beans>

No comments:

Post a Comment