static void Main(string[] args)
        {
            Composite root = new Composite("root");

            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite compX = new Composite("Composite X");

            compX.Add(new Leaf("Leaf XA"));
            compX.Add(new Leaf("Leaf XB"));

            root.Add(compX);

            Composite compY = new Composite("Composite Y");

            compY.Add(new Leaf("Leaf YA"));
            compY.Add(new Leaf("Leaf YB"));

            compX.Add(compY);

            root.Show(1);

            Console.Read();
        }
        public void Run()
        {
            Console.WriteLine("Composite Pattern");

            Composite root = new Composite("root");

            //add leaf to root
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite component = new Composite("Compnent X");

            component.Add(new Leaf("Comp Leaf XA"));
            component.Add(new Leaf("Comp Leaf XB"));

            //add component to root
            root.Add(component);
            //add a new leaf
            root.Add(new Leaf("Leaf C"));

            //Add and remove leaf
            Leaf leaf = new Leaf("Leaf D");

            root.Add(leaf);
            root.Remove(leaf);

            root.Show(1);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            //以树形结构为例展示组合模式
            Composite root = new Composite("树根");

            root.Add(new Leaf("叶子A"));
            root.Add(new Leaf("叶子B"));

            Composite composite1 = new Composite("节点1");

            composite1.Add(new Leaf("1叶子A"));
            composite1.Add(new Leaf("1叶子B"));

            Composite composite2 = new Composite("节点2");

            composite2.Add(new Leaf("2叶子A"));
            composite2.Add(new Leaf("2叶子B"));

            composite1.Add(composite2);
            root.Add(composite1);
            root.Show(1);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            //以树形结构为例展示组合模式
            Composite root = new Composite("树根");

            root.Add(new Leaf("叶子A"));
            root.Add(new Leaf("叶子B"));

            Composite composite1 = new Composite("节点1");

            composite1.Add(new Leaf("1叶子A"));
            composite1.Add(new Leaf("1叶子B"));

            Composite composite2 = new Composite("节点2");

            composite2.Add(new Leaf("2叶子A"));
            composite2.Add(new Leaf("2叶子B"));

            composite1.Add(composite2);
            root.Add(composite1);
            root.Show(1);

            //以学校为例展示组合模式
            ConcreteDepartmentcs rootSchool = new ConcreteDepartmentcs("辽宁科技大学");

            rootSchool.Add(new StudentOffice("辽宁科技大学学生管理处"));
            rootSchool.Add(new TeachOffice("辽宁科技大学教师管理处"));

            ConcreteDepartmentcs dep1 = new ConcreteDepartmentcs("软件学院");

            dep1.Add(new StudentOffice("软件学院学生管理处"));
            dep1.Add(new StudentOffice("软件学院教师管理处"));

            rootSchool.Add(dep1);
            rootSchool.ReportWork();
            rootSchool.Show(1);
        }