According to Should we avoid custom objects as parameters?, for example, if I have an object to show:
public class Student{
public int _id;
public String name;
public int age;
public float score;
}
and I need to pass the information of the object to a window to show, passing the whole object as parameter:
public class Window{
public void showInfo(Student student){
}
}
is better than passing the class members individually:
public class Window{
public void showInfo(int _id,String name,int age){
}
}
. However, what if the original parameter is already an object? Should I introduce another new parameter object? I found some drawbacks of "passing the whole object":
It introduces coupling between Window and Student
Window may access the class members of Student that it doesn't need to show (eg:score)
When there is another types of objects which is unrelated to Student, eg: Teacher ( which Teacher and Student has no common parent class), I need to add another method :
public void showInfo(Teacher teacher){
}
I want to gain the advantage of both "pass required fields only" and "pass the whole object", so I want to "introduce parameter object" to Student: add a ShowInfoParam to collect the required parameters:
public class ShowInfoParam{
public int _id;
public String name;
public int age;
}
public class Window{
public void showInfo(ShowInfoParam showInfoParam){
}
}
//some controller to show information of student
public onShowStudentButtonPressed(){
ShowInfoParam showInfoParam=new ShowInfoParam();
showInfoParam._id=this.student._id;
showInfoParam.name=this.student.name;
showInfoParam.age=this.student.age;
this.window.showInfo(showInfoParam);
}
//some controller to show information of teacher
public onShowTeacherButtonPressed(){
ShowInfoParam showInfoParam=new ShowInfoParam();
showInfoParam._id=this.teacher._id;
showInfoParam.name=this.teacher.name;
showInfoParam.age=this.teacher.age;
this.window.showInfo(showInfoParam);
}
My question is, is it necessary or over engineering to do so?