Understanding how to decouple

519 views Asked by At

I've been trying to keep coupling down in my code, but I think I may not fully understand it. My basic understanding is that coupling is "how dependent classes are on each other and know about the behavior of each other." I know that dependency injection is one way to reduce coupling and IoC.

The following is an quick example I came up of a Student, Professor, and Course. A course has a list of students and a professor. I have a controller (using MVC) that injects the Student and Professor objects. Would the following still be considered coupled, or tightly coupled? This also would be an example of DI, correct?

Student class

public class Student {
private String firstName;
private String lastName;
private int studentID;
private int address;
private int telephone;
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getStudentID() {
    return studentID;
}
public void setStudentID(int studentID) {
    this.studentID = studentID;
}
public int getAddress() {
    return address;
}
public void setAddress(int address) {
    this.address = address;
}
public int getTelephone() {
    return telephone;
}
public void setTelephone(int telephone) {
    this.telephone = telephone;
}
}

Professor Class

public class Professor {
private String firstName;
private String lastName;
private int professorID;
private int address;
private int telephone;
private int salary;
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getProfessorID() {
    return professorID;
}
public void setProfessorID(int professorID) {
    this.professorID = professorID;
}
public int getAddress() {
    return address;
}
public void setAddress(int address) {
    this.address = address;
}
public int getTelephone() {
    return telephone;
}
public void setTelephone(int telephone) {
    this.telephone = telephone;
}
public int getSalary() {
    return salary;
}
public void setSalary(int salary) {
    this.salary = salary;
}
}

Course Class

import java.util.List;

public class Course {
private List<Student> students;
private Professor professor;
public Professor getProfessor() {
    return professor;
}
public void setProfessor(Professor professor) {
    this.professor = professor;
}
public List<Student> getStudents() {
    return students;
}
public void setStudents(List<Student> students) {
    this.students = students;
}

}
2

There are 2 answers

0
n00b On

What you have put here seem to be the Model part of your MVC implementation which have limited functionality other than setter-getters. To have a good example of decoupling and DI, you probably have Model, View and Controller classes that implement some interfaces. They are decoupled as each class's implementation is not aware/dependent on the other implementations. They only relate based on interfaces that well encapsulate and isolate components.

Probably you also have some fake/test implementation of those interfaces too

Then, there is a IoC setup that controls what implementation of each component will be resolved.

0
AdamSkywalker On

I have a controller (using MVC) that injects the Student and Professor objects. Would the following still be considered coupled, or tightly coupled?

Since all references are Classes, you have a tightly coupled design. A good approach is to use interfaces in your code. This will allow you to change implementation any time you want and it will not affect the rest of your application.

This also would be an example of DI, correct?

If your Course, Professor and Student are configured beans, and you specify somewhere how to inject instances during bean instantiation, it will be a DI example. By now it is just three POJO classes.