I have a class in which I want to expand the functionality in the style of the decorator / adapter, but I do not want my expanding class to know about the things of the classes that it extends, and I do not want to write a new class for each type of object that I want to expand . However, all the objects that I would like to separate share a common base class, Team . It sounds ripe for generics, so here is my initial idea:
public class TournamentTeam<T> : T where T : Team { private int mSeed; public TournamentTeam(T team, int seed) : base(team) {
That would do what I wanted, since now, if I want to access members of T, the new class has all of them. The base constructor will take care of setting the state so that the extended class does not need it. I would also not need to know which methods should be redefined to point to an interior object in the style of the decorator / facade. Needless to say, this did not compile. So, I tried something completely different.
public class TournamentTeam<T> : Team where T : Team { #region class variables private int mSeed; private T mTeam; #endregion public TournamentTeam(int seed, T team) : base(team) {
OK, now, if I want to get the functionality of a βbase classβ, I just call the Team property, and I'm good to go. And since it is generic, I donβt need to do any kind of boxing to get to the functionality. It works, it is pretty beautiful, but it works.
What picture is this, if it exists, and what pitfalls do you see with this idea? Is there a better way to do this?
source share