DesignPattern_Structural_Composite

using System.Collections.Generic;

namespace DesignPattern.Structural.Composite
{
    public abstract class Component
    {
        public virtual void Show(int level) { }
        public abstract void Add(Component component);
        public abstract void Remove(Component component);

    }
    public class Composite : Component
    {
        public List<Component> Components { get; set; }

        public override void Add(Component component)
        {
            Components.Add(component);
        }

        public override void Remove(Component component)
        {
            Components.Remove(component);
        }

        public override void Show(int level)
        {
            base.Show(level);
            foreach (var component in Components)
            {
                component.Show(level + 1);
            }
        }
    }
    public class Leaf : Component
    {
        public override void Add(Component component)
        {
            throw new System.NotImplementedException();
        }

        public override void Remove(Component component)
        {
            throw new System.NotImplementedException();
        }
    }

}
时间: 2024-11-05 11:48:52

DesignPattern_Structural_Composite的相关文章