What design design would be like that, and is that a good idea? (FROM#)

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) { /* * error checking stuff here */ // set class variables this.mSeed = seed; } public int Seed { get { return this.mSeed; } } } 

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) { /* * error checking stuff here */ // set class variables this.mSeed = seed; this.mTeam = team; } public int Seed { get { return this.mSeed; } } public T Team { get { return this.mTeam; } } } 

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?

+4
source share
2 answers

this is what should be done, as I see "approve composition over inheritance" and the strategy template (if they are not mistaken)

I also like the implication.

+1
source

Unfortunately, C # does not allow inheriting from type parameters, because I think the first design is perfect, given what you want to achieve.

The second design seems like a reasonable adapter template application, provided you guarantee that all public TournamentTeam members inherited from the Team redirect their calls to the encapsulated team.

Personally, if I were to create this in C #, I would rip off the adapter template in favor of simple composition and do something like the following:

  • Rename TournamentTeam to something else ... maybe TournamentTeamAllocation or TournamentTeamInfo or something like that (the names are complicated!). In principle, he will be responsible for the data related to both the tournament and the team (i.e., Semenov)
  • Change it so that it does not subclass anything
  • Store the encapsulation so that it still contains T : Team .
+1
source

Source: https://habr.com/ru/post/1400792/


All Articles