Search Tutorials


Traditional approach without using Dependency Injection | JavaInUse



Traditional approach without using Dependency Injection

Overview

In the previous chapter we saw how to create a Maven project with required dependencies which will be used throughout this series. Before we start with Spring lets understand the Traditional approach of Development. In traditional programming approach the dependencies of a class are determined and instantiated at runtime as and when they are required. Thus when need arises we instantiate the dependency class using the new keyword. Using this approach has many disadvantages which we will see later.

Here we will implement an Employee Management system. The design will be as follows.

final_basic2-uml
The user can perform the functions of adding new employee, displaying all employees.
We will implement this system using Traditional approach.

Lets Begin

We will create Eclipse Maven project as follows-
final_basic2-1
We will first create our Domain Class Employee.java as follows
package com.javainuse.domain;

public class Employee {

    private String empId;
    private String name;
    private String designation;
    private double salary;

    public Employee() {
    }

    public Employee(String empId, String name, String designation, double salary) {
        super();
        this.setEmpId(empId);
        this.name = name;
        this.designation = designation;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

}
Next we will write EmployeeService.java. The EmployeeService.java has a dependency on the EmployeeDAO.java. So we create an instance of the EmployeeDAO class in the EmployeeService.java using the new keyword. The function of the EmployeeDAO class is to store employee details in DB and fetch these details when required from DB.

The EmployeeService.java has two methods.
  • addNewEmployee(Employee emp)- This method calls the EmployeeDAO class method to save the new Employee details.
  • getEmployees()- This method calls the EmployeeDAO class method to get the List of all employees.
package com.javainuse.service;

import java.util.List;
import com.javainuse.dao.EmployeeDAO;
import com.javainuse.domain.Employee;

public class EmployeeService {
    //insert dependency using new keyword
    EmployeeDAO empDAO = new EmployeeDAO();    

    public void addNewEmployee(Employee emp) {
        empDAO.addNewEmployee(emp);
    }

    public List<Employee> getEmployees() {
        return empDAO.getAllEmployees();
    }

}
Currently in this chapter we will not use any DB. We will mock the EmployeeDAO.java as follows to save the employee details in an Employee ArrayList and return the same. So our EmployeeDAO.java will be as follows-
package com.javainuse.dao;

import java.util.ArrayList;
import java.util.List;
import com.javainuse.domain.Employee;

public class EmployeeDAO {

    private List<Employee> testEmployees = new ArrayList<Employee>();

    public void addNewEmployee(Employee employee) {
        testEmployees.add(employee);
    }

    public List<Employee> getAllEmployees() {
        return new ArrayList<Employee>(testEmployees);
    }

}
Finally we will write MainApplication.java to run our program. We create new instances of Employee and add it to the new instance of EmployeeService.
package com.javainuse.main;

import java.util.List;
import com.javainuse.domain.Employee;
import com.javainuse.service.EmployeeService;

public class MainApplication {

    public static void main(String[] args) {
        EmployeeService empService = new EmployeeService();
        Employee emp1 = new Employee("1", "Test1", "Manager", 1000);
        Employee emp2 = new Employee("1", "Test2", "Manager", 1000);
        Employee emp3 = new Employee("1", "Test3", "Manager", 1000);
        Employee emp4 = new Employee("1", "Test4", "Manager", 1000);
        Employee emp5 = new Employee("1", "Test5", "Manager", 1000);

        empService.addNewEmployee(emp1);
        empService.addNewEmployee(emp2);
        empService.addNewEmployee(emp3);
        empService.addNewEmployee(emp4);
        empService.addNewEmployee(emp5);
        List<Employee>employees = empService.getEmployees();
        for (Employee employee : employees) {
            System.out.println(employee.getName());
        }

    }

}
On Running this program we get the output as
final_basic2-2
Disadvantages of this traditional approach-

  • The main disadvantage of this approach is the tight coupling between the classes. The EmployeeService and the
    EmployeeDAO are tightly coupled. If the EmployeeDAO is suppose replaced by another class in future then there will be code changes required in the EmployeeService class
  • The code becomes difficult to analyze as new user will have to check where all the instances are created for the dependencies.
  • Testing of the code is cumbersome since we cannot mock the classes for testing purpose.
  • Spring provides support for different frameworks like Hibernate which makes it easier to implement these frameworks. With traditional approach this is not the case.


Download Source Code

Download it - Employee Management System

What Next?

In the next chapter will implement Dependency Injection using Core Java.