Original Post
🔒 Locked
Multiple Inheritance?
Advertisement
Martin Fowler has written an article on "dealing with roles" which is worth reading.
I would use the composite pattern.
Each person can contain one or many roles.
In this case a person object would contain an player object and a coach object.
Then you could have a function "perform_responsibilities". In the player and coach classes it would do its specific responsibilities. In person it would call the "perform_responsibilities" function in its children.
http://en.wikipedia.org/wiki/Composite_pattern
Each person can contain one or many roles.
In this case a person object would contain an player object and a coach object.
Then you could have a function "perform_responsibilities". In the player and coach classes it would do its specific responsibilities. In person it would call the "perform_responsibilities" function in its children.
http://en.wikipedia.org/wiki/Composite_pattern
Quote:
Original post by Smit
Well you could have a base person class and then have a class for each profession (player, coach etc.) which inherit from person. If you then want a "Player Coach" you code do just created a new class that inherits from both player and coach.
I think I should you warn you this would lead to the Dreaded Diamond of Doom (TM).
It's all well and good that you can call methods of the Player and Coach classes, but when you call a method of the Person class, which gets called, the Player's Person class or the Coach's Person class?
You can explicitly define which of the two you want to use in your code, but it's pretty well documented that diamond inheritance should be avoided altogether, and there is always another (better) design you can use.
Do you really need a player class? After all depending on your implementation the player-coach is just an object of type coach.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement