使用通用列表作为属性 [英] Using a Generic List as a propery

查看:79
本文介绍了使用通用列表作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我试图在Code Project上寻找解决方案,但没有找到解决方案.因此,如果以前曾问过这个问题,请指出正确的方向.

好的,所以我试图在VS2010中使用C#实现复合设计模式结构.

首先,我使用的设计模式并不重要,但是我很好奇的是VS2010及其从我的结构生成的类图.

我创建了一个抽象的Component类,并让Composite类从中继承.我还具有从抽象的Component Class继承的Leaf Class.

为了在Component和Composite之间进行简单的聚合,可以使用ArrayList,但是我选择了通用列表.组件

.因此,现在我可以使用

  public   void  addChild(Component childComponent) 

将子级添加到我的Composite中.这有效并且很酷.但是我注意到(在VS2010中生成类图时),没有箭头(聚集)从Composite指向我的组件类.通过使用

List<Component> component

确实存在聚合.

然后,如果我继续并绘制从Composite到Component的聚合,则会在我的Composite类中生成以下代码:

  public 组件组件
{
    获取
    {
        抛出  System.NotImplementedException();
    }
    设置
    {
    }
}



所以,最后是我的问题:您如何将生成的代码与List< t>一起使用.甚至有必要为此担心吗?

解决方案

您的设计似乎适合简单的复合模式,并使用通用List< basetype>复合节点是正确的方法.如果图表工具无法理解,请使用更好的工具(例如笔和纸?).


如果它是abstract,则可以不能以您惯用的方式使用它.您必须从中派生一个新类,如下所示:

 公共  class  MyComponent:组件
{
  // 您必须覆盖此类中的所有纯虚拟(抽象)方法
} 


然后,您可以执行此操作:

 公共  class  MyComponentCollection:列表< MyComponent>
{
}
MyComponentCollection myComponents =  MyComponentCollection();
myComponents.Add(...); 



或这样:

列表< MyComponent> myComponents = 列表< MyComponent> ;;
myComponents.Add(...); 



我更喜欢第一个用法示例,因为它在codemonkey方面更干净.

编辑(注释后)======================

为了创建基于Component的对象的集合,该集合必须包含从组件派生的对象派生的对象(因为它是抽象的).我的建议是创建一个接口,或者将Component更改为不抽象的.您也可以考虑将addChild方法更改为接受Object而不是Component.然后,您就不需要抽象Component.


Hi all,

I tried to look for a solution on this on Code Project, but found none. So if this question has been asked before, please point me in the right direction.

Ok, so I am trying to implement a Composite Design Pattern structure using C# in VS2010.

Firstly, what I''m using the design pattern for is not important, but what I am curious about is VS2010 an its Class Diagrams that it generate from my structure.

I created an abstract Component Class and have the Composite Class inheriting from that. I also have the Leaf Class inheriting from the abstract Component Class.

For a simple aggregation between Component and Composite, one can use an ArrayList, but I opted for a generic list

List<Component> component

. So now I can use a method

public void addChild(Component childComponent)

to add children to my Composite. This works and is cool. But I noticed (when generating a class diagram in VS2010) that there is no arrow (aggregation) pointing to my Component Class from Composite. An aggregation does exist through the use of my

List<Component> component

.

If I then continue and draw an aggregation from Composite to Component, the following code is generated in my Composite Class:

public Component Component
{
    get
    {
        throw new System.NotImplementedException();
    }
    set
    {
    }
}



So, finally my question: How would you use this generated code with a List<t>. Is it even necessary to worry about this?

Thanks in advance.

解决方案

Your design seems fine for a simple composite pattern, and using a generic List<basetype> for the composite node is the right way to go. If the diagram tool doesn''t understand it, use a better one (like a pen and piece of paper? ;)).


If it''s abstract, you can''t use it the way you''re doiing it. You MUST derive a new class from it, like so:

public class MyComponent : Component
{
  // You MUST override all pure virtual (abstract) methods in this class
}


Then you can do this :

public class MyComponentCollection : List<MyComponent>
{
}
MyComponentCollection myComponents = new MyComponentCollection();
myComponents.Add(...);



or this:

List<MyComponent> myComponents = new List<MyComponent>;
myComponents.Add(...);



I prefer the first usage example because it''s cleaner on the codemonkey side.

EDIT (after comment) =======================

In order to create a collection of Component-based objects, the collection must contain objects that are derived from an object derived from component (because it''s abstract). My suggestion is to either create an interface, or change Component to not be abstract. You could also consider changing the addChild method to accept an Object instead of a Component. Then, you wouldn''t need to make Component abstract.


这篇关于使用通用列表作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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