这将是什么设计模式,这是一个好主意吗? (C#) [英] What design pattern would this be, and is it a good idea? (C#)

查看:89
本文介绍了这将是什么设计模式,这是一个好主意吗? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,希望以某种修饰器/适配器的方式扩展功能,只是我不希望我的扩展类对扩展的类有任何了解,并且我不想为要扩展的每种对象编写新的类。但是,我想扩展的所有对象都共享一个公共基类 Team 。这对于使用泛型来说似乎已经成熟,所以这是我的最初想法:

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, Team. This sounds ripe for a use of generics, so here was 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; }
   }
}

就像现在一样,这可以满足我的要求如果我想访问T的成员,则新类具有所有成员。基本构造函数将以某种方式设置状态,以使扩展类不必担心它。我也不必知道要重写什么方法才能以装饰器/外观类型的方式指向内部对象。不用说,它没有编译。因此,我尝试了一些不同的尝试。

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.

    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; }
        }
    }

好的,现在,如果我想去基类的功能,我只需调用Team属性,就可以了。而且,由于它是通用名称,因此我无需进行任何装箱操作即可使用该功能。

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.

如果存在的话,这是什么模式,这个想法会给你带来什么陷阱?

What pattern is this, if one exists, and what pitfalls do yall see with this idea? Is there a better way to accomplish this?

推荐答案

很遗憾,C#不允许从类型参数继承,因为我认为

It's unfortunate that C# doesn't allow inheritance from type parameters because I think the first design is ideal, given what you want to achieve.

第二个设计似乎是对适配器模式的合理应用,前提是您确保所有公共TournamentTeam成员都继承了

The second design seems like a reasonable application of the adapter pattern, provided that you ensure that all public TournamentTeam members inherited from Team have their invocations redirected towards the encapsulated Team.

就个人而言,如果我要使用C#设计,我会放弃适配器模式,而采用简单的构成,执行以下操作:

Personally, if I were to design this in C#, I'd ditch the adapter pattern in favour of plain composition and do something like the following:


  • 将TournamentTeam重命名为其他名称...也许TournamentTeamAllocation或TournamentTeamInfo之类的东西(名称很难!) 。基本上,它将负责处理与锦标赛和团队有关的数据(即种子)

  • 进行更改,以使其不会成为任何子类

  • 保持封装,使其仍然包含 T:团队

  • Rename TournamentTeam to something else... maybe TournamentTeamAllocation or TournamentTeamInfo or something like that (names are hard!). Basically, it'd be responsible for dealing with data related to both a tournament and a team (i.e. seeds)
  • Change it so that it doesn't subclass anything
  • Keep the encapsulation so that it still contains a T : Team.

这篇关于这将是什么设计模式,这是一个好主意吗? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆