java - Understanding how to decouple -
i've been trying keep coupling down in code, think may not understand it. basic understanding coupling "how dependent classes on each other , know behavior of each other." know dependency injection 1 way reduce coupling , ioc.
the following quick example came of student, professor, , course. course has list of students , professor. have controller (using mvc) injects student , professor objects. following still considered coupled, or tightly coupled? 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; } }
i have controller (using mvc) injects student , professor objects. following still considered coupled, or tightly coupled?
since references classes, have tightly coupled design. approach use interfaces in code. allow change implementation time want , not affect rest of application.
this example of di, correct?
if course, professor , student configured beans, , specify somewhere how inject instances during bean instantiation, di example. 3 pojo classes.
Comments
Post a Comment