Understanding how to separate

I am trying to keep the connection in my code, but I think I cannot fully understand it. My basic understanding is that the relationship is "how the dependent classes sit on top of each other and know about each other's behavior." I know that dependency injection is one way to reduce communication and IoC.

Below is a quick example: I came to student, professor and course. The course has a list of students and a professor. I have a controller (using MVC) that injects Student and Professor objects. Will it still be considered related or closely related? That would also be a DI example, right?

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's 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;
}

}

      

+3


source to share


2 answers


I have a controller (using MVC) that injects Student and Professor objects. Will it still be considered related or closely related?

Since all links are classes, you have a tightly coupled design. A good approach is to use interfaces in your code. This will allow you to change the implementation at any time, without affecting the rest of your application.



This would also be a DI example, right?

If your course, professor and student have beans configured and you are specifying somewhere how to inclinate instances during bean creation, this would be a DI example. So far, there are only three POJO classes.

+3


source


What you put here seems to be part of your MVC implementation model, which has limited functionality other than receivers. To have a good example of decoupling and DI, you probably have Model, View and Controller classes that implement some of the interfaces. They are decoupled as each implementation of the class is unaware / dependent on other implementations. They only refer to interfaces that encapsulate and isolate components well.

Perhaps you also have some fake / test implementations of these interfaces.



Then there is the IoC setting that controls which implementation of each component is allowed.

0


source







All Articles