Whose job is it to mutate an object? (Course Management project)
Let's say I've got two classes, a
Course
and a Student
class.
Let's say I want to add a Course
to a student
object. What's better design:
student.enroll(course)
or
CourseManager.enroll(student, course)
Basically should the responsibility of enrolling a course be given to the student or a separate CourseManager
class2 Replies
Seeing how the course would probably have a list of students, I'd say adding a student to a course is more natureal
Basically should the responsibility of enrolling a course be given to the student or a separate CourseManager classThis is a very common pattern, I dont know if it really has a specific name, but its a Seperation of Concerns way to organize stuff where you have "Is a" objects/pocos/dtos/models (whatever you wanna call em), and then "does a" services/managers/repositories/etc (whatever you wanna call em) It's a lot easier to unit test when you very concretely break your logic apart into the "is" vs "does" logic nice and neatly organized Service Model pattern?