开发者

simple design problem : parent, child ; teacher,student

very simple problem, but i want to see how experts look at it.

This is just imaginary software just to understand OOP. I have a school admin开发者_JS百科istration software. So I have classes

  • Student
  • ClassRoom
  • Teacher

Now I assign a teacher as class-teacher for a particular classroom. Thus ClassRoom contains

Teacher classTeacher; 
Student[] students; 

Now the complete program is written and everything works fine ...

Problem:

Now what If the principal puts a new rule, that there should not exist a student to whom the Teacher is the Parent. Now we need to bring more parameters and lot of changes in our code for a small requirement. How should the design be in order for future changes in requirements (which will inevitably come) to only require minor changes in the code?


This should not be a big code change, this should be a validation change.

Psuedo-Code:

Class ClassRoom {
    List<Student> students
    Teacher teacher

    ClassRoom(Teacher _teacher, List<Student> students) {
        teacher = _teacher;
        SetStudents(students);
    }

    void SetStudents(List<Student> _students) {
         foreach (Student s in _students) {
             if (validate(s)) {
                 students.add(s);
             } else {
                 // error handling logic
             }
         }
    }

    void validate(Student student) {
        if (student.parent == teacher) {
            return false;
        }
        return true;
    }
}

class Student : Person {
    Person parent
}

class Teacher : Person { }

Sure you are going to have to go into the database and the codebase and add "Parent" properties to all your students. But that also shouldn't be too hard.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜