September 19, 2013

How to reduce coupling in java

 

How to reduce coupling in java

 Description : This examples shows how the concept of interface in java helps to reduce the coupling between classes/ decoupling the classes, Inversion of control, Removing dependency injection

uml diagram example in java- How to reduce coupling
UML Diagram

The concept is very simple. We are just removing the dependencies of class in another class. Let us check how will do this. By doing this project maintenance will be very easy since there is no dependencies. so please try this code

Copy and paste this source code :
This program shows interface example in java

EmployeeInterface.java
/**
* FileName : EmployeeInterface.java
* Author : belazy1987atgmailcomcom
* Date : September 19 1987
* Description : Interface of the project
*/
package com.nick.Interface;
/**
* @author Oscar
* Implementing Employee Interface to types of employees in java
*/
public interface EmployeeInterface {
/**
* basic salary of the employee
*/
int BASICS = 5000;
/**
* house rent allowance for an employee
*/
int HR = 3000;
/**
* Traveling allowance for an employee
*/
int TA = 2000;
/**
*
*/
int DA = 4000;
/**
* @return int
* abstract method for getting the salary
*/
public int getSalary();
/**
*
* @return String
* abstract method for finding the desingation of employee
*/
public String getDesignation();
}
HumanResourceManager.java
/**
* FileName : HumanResourceManager.java
* Author : belazy1987@gmail.com
* Date : September 29 1987
* Description : Represents the details of Human Resource Manager
*/
package com.nick.Interface;
/**
* @author Oscar
*
*/
public class HumanResourceManager implements EmployeeInterface {
/**
* @return string
* Defines the desingation of the employee
*/
@Override
public String getDesignation() {
String designation = ” HumanResourceManager”;
return designation;
}
/**
* @return int
* calculate the salary of the employee (basic + hr)
*/
@Override
public int getSalary() {
int salary = BASICS + HR;
return salary;
}
}
BusinessAnalyst.java
/**
* FileName : BusinessAnalyst.java
* Author : menijesh@gmail.com
* Date : September 29 1987
* Description : MainClass of project
*/
package com.nick.Interface;
/**
* @author Oscar
*
*/
public class BusinessAnalyst implements EmployeeInterface{
/**
* @return string
* defines the designation of the employee
*/
@Override
public String getDesignation() {
String designation = “BusinessAnalyst”;
return designation;
}
/**
* @return int
* calculate the salary of the employee (basic + hr + da)
*/
@Override
public int getSalary() {
int salary = BASICS + HR + DA ;
return salary;
}
}
MarketingStaff.java
/**
* FileName : MarketingStaff.java
* Author : menijesh@gmail.com
* Date : September 29 1987
* Description : Represent the details of marketting staff
*/
package com.nick.Interface;
/**
* @author Oscar
*
*/
public class MarketingStaff implements EmployeeInterface {
/**
* @return string
* defines the designation of the employee
*/
@Override
public String getDesignation() {
String designation = “Marketing”;
return designation;
}
/**
* @return int
* calculate the salary of the employee (basic + hr + ta)
*/
@Override
public int getSalary() {
int salary = BASICS + HR + TA;
return salary;
}
}
MainClass.java
/**
* FileName : MainClass.java
* Author : menijesh@gmail.com
* Date : September 29 1987
* Description : MainClass of project
* Main Idea of the project is to describe how
* interface works and how to reduce coupling
*/
package com.nick.Interface;
/**
* @author belazy1987@gmail.com
* Working with interface in java
*/
public class MainClass {
/**
* @param cmdArg
*/
public static void main(String[] computerScience) {
EmployeeInterface employeeInterface = null;
employeeInterface = new HumanResourceManager();
System.out.println(“Designation :”+employeeInterface.getDesignation() +” and Salary :”+employeeInterface.getSalary());
employeeInterface = new BusinessAnalyst();
System.out.println(“Designation :”+employeeInterface.getDesignation() +” and Salary :”+employeeInterface.getSalary());
employeeInterface = new MarketingStaff();
System.out.println(“Designation :”+employeeInterface.getDesignation() +” and Salary :”+employeeInterface.getSalary());
}
}

The Above program is an example for interface in java. Why java doesnt support multiple inheritence?

This example shows how to reduce couple or enable loose coupling through concept of interface in java. 

 



Facebook comments