简体   繁体   English

C#:子类列表 <Something> ?

[英]C#: Subclass List<Something>?

I have a class EqualCondition which implements my own interface ICondition , which has only one method: SatisfiedBy(Something) . 我有一个类EqualCondition它实现我自己的接口ICondition ,它只有一个方法: SatisfiedBy(Something)

public class EqualCondition : ICondition {
    private Something m_Something;

    public HelloCondition(Something something) {
        m_Something = something;
    }

    // Magic!!!
    public bool SatisfiedBy(Something something) {
        return something == m_Something;
    }
}

So ICondition is very simple to implement. 因此, ICondition实现非常简单。 Now I'm trying to create a CombinationCondition which also implements it. 现在,我正在尝试创建一个同时实现它的CombinationCondition The idea is that CombinationCondition which will contain a list of ICondition s which will determine whether SatisfiedBy will be successful or not. 这个想法是CombinationCondition将包含ICondition的列表,该列表将确定SatisfiedBy是否成功。

My first thought was to make CombinationCondition implement IList<Something> but I quickly realized that I was only duplicating List<Something> . 我的第一个想法是使CombinationCondition实现IList<Something>但是我很快意识到我只是在复制List<Something> So why not just subclass it? 那么,为什么不只是继承它呢?

That idea sounded fine until I started thinking again about how to implement SatisfiedBy if I just subclassed List<Something> . 这个想法听起来不错,直到我再次考虑如果我只是将List<Something>子类化了,如何实现SatisfiedBy I need to do: 我需要去做:

return innerList.All(x => x.SatisfiedBy(something))

But how do I access the inner list? 但是,如何访问内部列表?

Personally, for the use case you're showing, I would just make this implement IEnumerable<Condition> . 就您所显示的用例而言,我个人将使此实现为IEnumerable<Condition> You could then just implement the GetEnumerator by calling the (internal, encapsulated) List<Condition> 's method. 然后,您可以通过调用(内部封装的) List<Condition>的方法来实现GetEnumerator。

Potentially, ICollection<Condition> may make more sense (so you can add conditions at runtime), but only if you need that capability. ICollection<Condition>可能更有意义(因此您可以在运行时添加条件),但是仅在需要此功能时。 Implementing IList<T> seems like overkill in this situation, for the use cases I'd see with this. 对于这种情况,在这种情况下,实现IList<T>似乎有点过头了。

From what you have posted, I would just have CombinationCondition contain (encapsulate) a List<Something> . 从您发布的内容来看,我只会让CombinationCondition包含(封装)一个List<Something> No need for the outside world to know it is a list unless absolutely necessary. 除非绝对必要,否则外界无需知道这是一个清单。

Edit 1: 编辑1:

public class CombinationCondition : ICondition {
private List<ICondition> list;

public CombinationCondition(List<ICondition> list) {
    this.list = list;
}

// if you need it
public void AddCondition( ICondition condition ){
    list.Add( condition );
}

// Still Magic!!!
public bool SatisfiedBy(Something something) {
    return list.Any( x => x.SatisfiedBy( something ) );
}

} }

Edit 2: 编辑2:
You might also consider renaming CombinationCondition to CompoundCondition ...makes more sense, at least to me :) 您可能还考虑将CombinationCondition重命名为CompoundCondition ...更有意义,至少对我而言:)

I'm not sure I 100% understand what you are trying to do, but would this solve your need? 我不确定我100%理解您要做什么,但是这可以解决您的需求吗?

public interface ICondition<T>
{
   bool SatisfiedBy(T something);
}

That way, you can just implement it for any generic type you need 这样,您可以将其实现为所需的任何通用类型

One possibility would be a property of type IList<ICondition> called maybe "Conditions". 一种可能是类型为IList <ICondition>的属性,可能称为“ Conditions”。

You don't need to access the inner list - you could access your class "itself". 您不需要访问内部列表-您可以访问“自身”类。

However, prefer sublassing from ICollection<T>. 但是,更喜欢从ICollection <T>进行子分类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM