Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat design pattern would this be, and is it a good idea? (C#)
    text
    copied!<p>I've got a class that I want to extend functionality to in a decorator/adapter kind of a way, only I don't want my extending class to have to know anything about the kinds of classes it is extending, and I don't want to have to write a new class for each type of object I want to extend. All objects I would want to extend off of, though, share a common base class, <code>Team</code>. This sounds ripe for a use of generics, so here was my initial idea:</p> <pre><code>public class TournamentTeam&lt;T&gt; : 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; } } } </code></pre> <p>This would have done what I wanted, as now if I want to access members of T, the new class has all of them. The base constructor would take care of setting up the state in a way such that the extended class didn't need to worry about it. I also wouldn't have to know what methods to override in order to point to an internal object in a decorator/facade type manner. Needless to say, this didn't compile. So, I tried something a little different.</p> <pre><code> public class TournamentTeam&lt;T&gt; : 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; } } } </code></pre> <p>OK, now if I want to get at the functionality of the "base class", I just call the Team property and I'm good to go. And, since it's generic, I don't have to do any boxing to get to the functionality. it works, it aint pretty, but it works.</p> <p>What pattern is this, if one exists, and what pitfalls do yall see with this idea? Is there a better way to accomplish this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload