0

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":

  1. It introduces coupling between Window and Student

  2. Window may access the class members of Student that it doesn't need to show (eg:score)

  3. 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?

1 Answer 1

1

I would turn your Window class around a bit and add a ViewModel rather than pass Student and Teacher or parameters into the method.

The view model then becomes your parameter object, but it has a well defined scope and reason for being, preventing the proliferation of random parameter classes.

eg.

public class PersonDisplayWindow
{
   PersonDisplayViewModel ViewModel;
   public PersonDisplayWindow(PersonDisplayViewModel ViewModel) { .. inject the view model}
   
   OnLoad()
   {
      this.TextBox1.Text = ViewModel.Name;
      ....
   }

}

public class MainWindow
{
   OnSelectedStudent() //assume two selection boxes
   {
     this.personWindow = new PersonWindow(new PersonDisplayViewModel () { Name = selectedStudent.Name ...});
     this.personWindow.Display();
   }

   OnSelectedTeacher()
   {
     ..same but with selected teacher
   }
}

The View Model might wrap Teacher and Student objects, or you might have a base Person class, but where it shows its worth is when you have unrelated properties such as "highlighted" or "IsSelected" or whatever which only relate to the view, not the Person its displaying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.