分公司 = 一部门 组合模式

19.1 分公司不就是一部门吗?

整体与部分可以被一致对待,

19.2 组合模式

将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性,

namespace 组合模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Composite root = new Composite("root");                    //生成树根root,跟上长出两叶LeafA和LeafB,
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite comp = new Composite("Composite X");             //根上长出分枝CompositeX,分枝上也有两叶LeafXA和LeafXB,
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));
            root.Add(comp);

            Composite comp2 = new Composite("Composite XY");           //在CompositeX上在长出分枝CompositeXY,分枝上也有两叶LeafXYA和LeafXYB,
            comp2.Add(new Leaf("Leaf XYA"));
            comp2.Add(new Leaf("Leaf XYB"));
            comp.Add(comp2);

            root.Add(new Leaf("Leaf C"));                              //根部又长出两叶LeafC和LeafD,可惜LeafD没长牢被风吹走了,
            Leaf leaf = new Leaf("Leaf D");
            root.Add(leaf);
            root.Remove(leaf);

            root.Display(1);

            Console.Read();
        }
    }

    //Component为组合中的对象接口声明,
    //适当情况下,实现所有类共有接口的默认行为,
    //声明一个接口用于访问和管理Component的子部件,
    abstract class Component
    {
        protected string name;

        public Component(string name)
        {
            this.name = name;
        }

        public abstract void Add(Component c);              //通常用Add和Remove方法来提供增加和移除树枝或树叶的过程,

        public abstract void Remove(Component c);

        public abstract void Display(int depth);
    }

    //Composite定义有枝节点行为,用来存储子部件,
    //在Component接口中实现与子部件有关的操作,如Add与Remove,
    class Composite : Component
    {
        //一个子对象集合用来存储其下属的枝节点和叶节点,
        private List<Component> children = new List<Component>();

        public Composite(string name)
            : base(name)
        { }

        public override void Add(Component c)
        {
            children.Add(c);
        }

        public override void Remove(Component c)
        {
            children.Remove(c);
        }

        //显示其枝节点名称,并对其下级进行遍历,
        public override void Display(int depth)
        {
            Console.WriteLine(new String('-', depth) + name);

            foreach (Component component in children)
            {
                component.Display(depth + 2);
            }
        }
    }

    //表示叶节点对象,叶节点没有子节点,
    class Leaf : Component
    {
        public Leaf(string name)
            : base(name)
        { }

        public override void Add(Component c)                             //由于叶子没有在再增加分枝和树叶,所以Add和Remove方法实现它没有意义,
        {                                                                 //但这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具备完全一致的接口,
            Console.WriteLine("Cannot add to a leaf");
        }

        public override void Remove(Component c)
        {
            Console.WriteLine("Cannot remove from a leaf");
        }

        public override void Display(int depth)                           //叶节点的具体方法,
        {
            Console.WriteLine(new String('-', depth) + name);
        }
    }

}
View Code

19.3 透明方式与安全方式

19.4 何时使用组合模式

19.5 公司管理系统

19.6 组合模式的好处

猜你喜欢

转载自www.cnblogs.com/huangxuQaQ/p/11301699.html