Implement IEnumerable for Composite Template Sheet Nodes

I implemented a compound template as follows

public interface IComponent
{
   string Name { get; }
}

public interface IComposite : IComponent
{
    void AddRange(IEnumerable<IComponent> components);
}
public interface ILeaf : IComponent
{
    string Content { get; }
    string Parent { get; }
}

public class Composite : IComposite
{
    // return an iterator?
    private readonly List<IComponent> _children = new List<IComponent>();

    public Composite(string name)
    {
        Name = name;
    }

    public string Name { get; }

    public void AddRange(IEnumerable<IComponent> components)
    {
        _children.AddRange(components);
    }
}

public class Leaf : ILeaf
{
    public string Name { get; }
    public string Content { get; }
    public string Parent { get; }

    public Leaf(string name, string content, string parent)
    {
        Name = name;
        Content = content;
        Parent = parent;
    }
}

I filled the composite from the xml file as follows

var collection = XElement.Load(@"C:\somexml.xml");
   var composite = CreateComposite(collection);

Where

public IComponent CreateComposite(XElement element)
    {
        if (!element.HasElements)
            return new Leaf(element.Name.LocalName, element.Value, element.Parent.Name.LocalName);

        var composite = new Composite(element.Name.LocalName);
        composite.AddRange(element.Elements().Select(CreateComposite));
        return composite;
    }

It fills my squad as expected - great! However, now I would like my composite component to return an iterator through an IEnumerable implementation. So i tried this

public class Composite : IComposite, IEnumerable<IComponent>
{
    // return an iterator?
    private readonly List<IComponent> _children = new List<IComponent>();

    public Composite(string name)
    {
        Name = name;
    }

    public string Name { get; }

    public void AddRange(IEnumerable<IComponent> components)
    {
        _children.AddRange(components);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public IEnumerator<IComponent> GetEnumerator()
    {
        foreach (var child in _children)
        {
            yield return child;
        }
    }
}

But this is only an iteration through the upper level of components, i.e. any components nested in _childrenare not returned. How to update this to recursively iterate over all components?

+4
source share
2 answers

You can repeat this recursively iteratively (it will do the depth iteration first):

public interface IComposite : IComponent, IEnumerable<IComponent>
{
    void AddRange(IEnumerable<IComponent> components);
}

public IEnumerator<IComponent> GetEnumerator()
{
    foreach (var child in _children)
    {
        yield return child;
        var composite = child as IComposite;
        if (composite != null) {
            foreach (var sub in composite) {
                yield return sub;
            }
        }
     }
}

IComposite - Composite IComposite . ILeft IComposite .

+3

Linq .

public IEnumerable<IComponent> GetSuccessors()
{
    return _children
           .Concat(_children.SelectMany(iChild => iChild.GetSuccessors());
}

depht-first traversal, .

public IEnumerable<IComponent> GetSuccessors()
{
    return _children
           .SelectMany(iChild => new IComponent[]{iChild}.Concat(iChild.GetSuccessors()));
}

, , , .

public IEnumerator<IComponent> GetEnumerator()
{
    var Successors
        = _children
          .SelectMany(iChild => new IComponent[]{iChild}.Concat(iChild.GetSuccessors()));
    foreach (var iSuccessor in Successors)
    {
        yield return iSuccessor;
    }
}
+3

Source: https://habr.com/ru/post/1674295/


All Articles