Composite template simplified

What am I losing without implementing the Component and considering everything as a composite?

I refused the implementation for Leaf node:

those.

class Component : IComponent
{ 
    /*...*/ 
}

Now plz take a look at my code.

public interface IComponent
{
    int ID { get;set; }
    string Name { get;set;}
    void Add(IComponent item);
    void Remove(IComponent item);
    List<IComponent> Items { get; }
    void Show();        
}


public class Composite : IComponent
{
    private int _id;
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public Composite(int id, string name)
    {
        _id = id;
        _name = name;
    }

    private List<IComponent> _items = new List<IComponent>();

    public void Add(IComponent item)
    {
        _items.Add(item);
    }

    public void Remove(IComponent item)
    {
        _items.Remove(item);
    }

    public List<IComponent> Items
    {
        get
        {
            return new List<IComponent>(_items);
        }
    }

    public void Show()
    {
        Console.WriteLine("ID=" + _id + "; Name=" + _name);
    }        
}


class Program
{
    static void Main(string[] args)
    {
        IComponent root = new Composite(1, "World");

        IComponent asia = new Composite(2, "Asia");
        IComponent europe = new Composite(3, "Europe");

        root.Add(asia);
        root.Add(europe);

        asia.Add(new Composite(4, "China"));
        asia.Add(new Composite(5, "Japan"));

        europe.Add(new Composite(6, "Germany"));
        europe.Add(new Composite(7, "Russia"));

        root.Show();
        Program.Traverse(root.Items);

        Console.ReadLine();
    }

    static void Traverse(List<IComponent> items)
    {            
        foreach (IComponent c in items)
        {
            c.Show();

            Traverse(c.Items);
        }
    }
}

What is wrong with this Composite Pattern approach? What problem can I face with this type of design?

+3
source share
2 answers

You refuse the subclass "Sheet", if it turns out that you have different types of "nodes", you are likely to end up polluting the structure one way or another. And you violate the principle of solitary accusation. It is very easy to get pollution of all kinds with a complex structure, and I think it always pays off to make it clean.

+7
source

, node, .

node, , node.
,

private List<IComponent> _items = new List<IComponent>();

    public void Add(IComponent item)
    {
        _items.Add(item);
    }

    public void Remove(IComponent item)
    {
        _items.Remove(item);
    }

    public List<IComponent> Items
    {
        get
        {
                return new List<IComponent>(_items);
        }
    }

ControlCollection , Control.

, , , .

. :)

: , , -, . .

, .net , Control, ControlCollection .

EDIT2: , , IComponent, IList<IComponent>, , , add/remove.

EDIT3:.net . dofactory , - node (, node).

EDIT4: dofactory, node, NotImplementedException add/`Remove '.

0

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


All Articles