Design pattern to add a new class
I have two classes, Student and Teacher, Student has one concrete method: takeCourse开发者_如何学Python; Teacher has one concrete method: teachCourse.
Now I want to add a new Class, GradStudent, which can take course, like Student, and also can teach course, like Teacher. Which pattern is the easiest way to implement this new class ? Adapter, Composite, Delegate .... ?
Thanks.
The question should be what is the best way to model this for which the answer would be "the way it makes sense in the real world" i.e. A Grad student can also teach and take course ;-)
Do not try to relate everything to the design patterns. Stick to the basic principle that we learnt in school "Classes model real world objects", "Keep the model close to the real world"
interface CanTeach
{
void teachCource();
}
class Teacher implements CanTeach {...}
class Student
{
void takeCourse(...);
}
class GradStudent extends Student implements CanTeach
{
...
}
精彩评论