2 ms·
The various SO answers combined sum it up. - Single inheritance means it's unnatural to inherit from the player list which is just one aspect of a team. - It'
by alkonaut 13y ago
The various SO answers combined sum it up.
- Single inheritance means it's unnatural to inherit from the player list which is just one aspect of a team.
- It's a leak of implementation detail. Tomorrow you may want the team to contain a sorted collection, or a set, or a sorted set.
- It gives an unnatural syntax to team.count vs team.players.count. Again, composition wins in clarity
- You can still expose any part of a collection interface in your Team class, e.g. team.Add(player). The details of what this does should be hidden. Adding an interface like IEnumerable<Player> to the team gives a nice syntax like "foreach(Player p in team)" without breaking encapsulation.
- A list is a simple data structure, a team is not. Extending something to completely change what it is usually means you have an unnatural inheritance. A special list (sorted, cirular...) can extend list in a natural way, a business object should probably "have a" rather than "be a" list.
- ollysb 13y ago>> Adding an interface like IEnumerable<Player> to the team gives a nice syntax like "foreach(Player p in team)" It would be interesting to allow _private_ inheritance, i.e. a class inherits methods from a superclass but they're all private and you need to add interfaces to expose them. That way you could inherit an implementation without leaking it into the public interface.