proper OO way

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Hi,
classTeacher contains a list of classChild
ClassChild contains methods, props and so on
Most things a classChild can do on its own.
But AskPermission cannot be done by the Child, but must be done by the
teacher.
Child has to ask its teacher to do that.
Is it a proper OO way to have a reference to the teacher to call a
teachermethod?
Thanks
Frank
 
Frank said:
classTeacher contains a list of classChild
ClassChild contains methods, props and so on
Most things a classChild can do on its own.
But AskPermission cannot be done by the Child, but must be done by the
teacher.
Child has to ask its teacher to do that.
Is it a proper OO way to have a reference to the teacher to call a
teachermethod?

Simple solution would be to pass a pointer of a Teacher to every Child
during construction/initialization.

Pure solution would be to have abstract class "PermissionGranter" that
Teacher should inherit, so that children should have a pointer of that
class instead of class Teacher.

Complex solution would be for all children to have a pointer to
"PermissionDispatcher" that will call a function of an appropriate
PermissionGranter according to the Child's properties, current time of
day, or whatever.

In all cases every Child should have some kind of pointer so that it
actually can AskPermission. You should always keep things as simple as
possible, so I recommend the simple solution. Since the Teacher contains
the list of children the complex solution doesn't make sense anyway.
 
Back
Top