实现递归聚合

时间:2014-02-21 13:54:17

标签: c# aggregation

我正在创建一个需要在其中一个类中使用递归聚合的应用程序。手头的类是“组件”,组件可以由存储在列表中的子组件组成。以下代码显示了这一点。

    public class Component {
//vars for class
public String componentName;
public String componentShape;
public String componentColour;
public String componentMaterial;
public int numChildComps;

//list to store child components of component
public List<Component> childComponents;

public Component(string _componentName, string _componentShape, string _componentColour, string _componentMaterial, int _numChildComps)
{
    componentName = _componentName;
    componentShape = _componentShape;
    componentColour = _componentColour;
    componentMaterial = _componentMaterial;
    numChildComps = _numChildComps;

    //if component has no child components set list to null
    if (numChildComps == 0)
    {
        //instatiate new list
        childComponents = new List<Component>();
        childComponents = null;
    }
    else if(numChildComps != 0)//if not null then create child components for the amount stated above.
    {
        childComponents = new List<Component>();
        for (int i = 0; i < numChildComps; i++)
        {
            Console.WriteLine("Add details for child component " + (i+1));
            Console.WriteLine("Enter component Name: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter shape: ");
            string shape = Console.ReadLine();
            Console.WriteLine("Enter Colour: ");
            string colour = Console.ReadLine();
            Console.WriteLine("Enter Material: ");
            string material = Console.ReadLine();
            Console.WriteLine("Enter num child components: ");
            string num = Console.ReadLine();
            childComponents.Add(new Component(name, shape, colour, material, Int16.Parse(num)));//instatiate new child component with params and add to the list.
        }
    }
}

这将实现一个类,如果子组件的数量参数大于0,那么它将创建该对象并将其存储在列表“childComponents”中。这很好用。我的问题是如何检索列表中的项目。以下面的例子为例,我有一个由一个组件组成的模型,但该组件有2个组件,其中一个组件有另外两个组件,其中一个组件有一个组件:

Model
 -component
  -childComponent
    -childComponent
    -childComponent
      -childComponent
  -childComponent

显然这可以永远持续下去,我已经尝试创建一段代码来淘汰所有组件和子组件,但它没有工作,因为你需要知道模型所具有的组件总量,然后是组件childComponents和等等。

我尝试过的代码(不对上面的例子进行建模)

    IEnumerable<SEModel> modelres = from SEModel sm in database
                                    select sm;
    foreach (SEModel item in modelres)
    {
      Console.WriteLine(item.getSetModelName);
      Console.WriteLine(item.componentList.First().componentName);
      foreach (SEComponent citem in item.componentList)
      {
       Console.WriteLine(citem.childComponents.First().componentName);
        foreach (SEComponent scitem in citem.childComponents)
        {
          Console.WriteLine(scitem.componentName);
        }
     }

如上所述,您必须知道子组件的组件数量,然后是子组件等等。

2 个答案:

答案 0 :(得分:2)

public IEnumerable<Component> GetComponents()
{
    yield return this;

    foreach( var childComp in childComponents )
    {
        foreach( var comp in childComp.GetComponents() )
        {
            yield return comp;
        }
    }
}

如果您不确定yield return是什么,read this

答案 1 :(得分:0)

如果您需要将组件检索为平面列表,可以这样做:

public List<Component> GetAllComponents(List<Component> components)
{
   var result = new List<Component>();
   result.AddRange(components);

   foreach (var component in  components)
        result.AddRange(GetAllComponents(component.childComponents));

   return result;
}

var allComponents = GetAllComponents(Model.Components);

虽然我不确定你的问题到底想要做什么。