public static void BaseSample()
        {
            Component root = new Composite();

            root.Add(new Leaf {
                Name = "Leaf 1"
            });
            root.Add(new Leaf {
                Name = "Leaf 2"
            });

            var com = new Composite();

            root.Add(new Leaf {
                Name = "Leaf 10"
            });
            root.Add(new Leaf {
                Name = "Leaf 11"
            });

            var newCom = new Composite();

            newCom.Add(new Leaf {
                Name = "Leaf101"
            });
            newCom.Add(new Leaf {
                Name = "Leaf102"
            });

            com.Add(newCom);

            root.Add(com);
            var leafA = new Leaf {
                Name = "Leaf A"
            };

            root.Add(leafA);

            root.Add(new Leaf {
                Name = "Leaf D"
            });

            root.Display();

            Console.WriteLine("再次展示");

            root.Remove(leafA);
            root.Display();
            Console.ReadLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // Create a tree structure
            Composite root = new Composite("root");

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

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

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

            root.Add(comp);
            root.Add(new Leaf("Leaf C"));

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

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

            // Recursively display tree
            root.Display(1);

            // Wait for user
            Console.ReadKey();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Composite root = new Composite("root");

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

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

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

            root.Add(comp);
            root.Add(new Leaf("Leaf C"));

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

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

            // Recursively display tree
            root.Display(1);
            Console.ReadLine();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Component component = new Composite();

            Component technology = new Composite()
            {
                Name = "net技术"
            };
            Component program = new Composite()
            {
                Name = "编程语言"
            };

            // 一级
            component.Add(technology);
            component.Add(program);
            // 二级
            technology.Add(new Leaf()
            {
                Name = "net新手"
            });
            technology.Add(new Leaf()
            {
                Name = "C#"
            });
            program.Add(new Leaf()
            {
                Name = "Java"
            });


            component.Display(2);
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Component root = new Composite("ROOT");

            root.Add(new Leaf("leaf1"));
            root.Add(new Leaf("leaf2"));

            Composite composite = new Composite("Composite1");

            composite.Add(new Leaf("leaf 1-1"));
            composite.Add(new Leaf("leaf 1-2"));


            root.Add(composite);

            Composite composite2 = new Composite("composite");

            composite2.Add(new Leaf("leaf 2-1"));
            composite2.Add(new Leaf("leaf 2-2"));

            composite.Add(composite2);


            root.Add(new Leaf("LEAF ONE"));
            root.Add(new Leaf("LEAF TWO"));

            root.Display(1);

            Console.Read();
        }
        static void Main(string[] args)
        {
            IComponent <string> Album = new Composite <string>("Album");

            Console.WriteLine("New Album Created");
            Console.WriteLine("Add Photos to Album");
            Album.Add(new Component <string>("Photo1"));
            Album.Add(new Component <string>("Photo2"));
            Album.Add(new Component <string>("Photo3"));
            Album.Add(new Component <string>("Photo4"));
            Console.WriteLine("Photos displayed after  being added");
            Console.WriteLine(Album.Display(4));
            Console.WriteLine("Delete Photo from Album");
            Album.Remove("Photo2");
            Console.WriteLine("Photos displayed after  being being deleted");
            Console.WriteLine(Album.Display(4));
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Composite root  = new Composite("模型库");
            Composite robot = new Composite("机器人");

            robot.Add(new Leaf("机器人1"));
            robot.Add(new Leaf("机器人2"));
            root.Add(new Leaf("车床"));
            root.Add(new Leaf("加工中心"));
            root.Add(robot);
            root.Add(new Leaf("立库"));

            root.Display(1);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            Composite root = new Composite("Root");

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

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

            com.Add(new Leaf("Leaf XA"));
            com.Add(new Leaf("Leaf XB"));
            root.Add(com);
            root.Display(1);
            Console.ReadKey();
        }
Exemple #9
0
        public static void Main(string[] args)
        {
            #region Tree

            var root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            var co = new Composite("CompositeA");
            co.Add(new Leaf("Leaf X"));
            co.Add(new Leaf("Leaf Y"));
            var co1 = new Composite("CompositeA");
            co1.Add(new Leaf("Leaf P"));
            co1.Add(new Leaf("Leaf Q"));

            co.Add(co1);
            root.Add(co);
            root.Display(0);

            #endregion Tree

            #region Company

            Company company        = new ConcreteCompany("华隆总公司");
            var     huaDongCompany = new ConcreteCompany("华隆华东分公司");
            var     huaBeiCompany  = new ConcreteCompany("华隆华北分公司");
            company.Add(huaDongCompany);
            company.Add(huaBeiCompany);

            huaDongCompany.Add(new HrDepartment("华隆华东分公司Hr部门"));
            huaDongCompany.Add(new FinanceDepartment("华隆华东分公司财务部门"));

            huaBeiCompany.Add(new HrDepartment("华隆华北分公司Hr部门"));
            huaBeiCompany.Add(new FinanceDepartment("华隆华北分公司财务部门"));

            company.Display(0);
            company.LineOfDuty();

            #endregion Company

            Console.ReadLine();
        }
Exemple #10
0
        static void Main(string[] args)
        {
            Composite Root = new Composite("root");  //The added name

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

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

            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));
            Root.Add(comp);
            Root.Add(new Leaf("Leaf C"));

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

            Root.Add(leaf);
            Root.Remove(leaf);

            // Recursively display Tree
            Root.Display(1);
            Console.ReadKey();
        }
Exemple #11
0
        static void Main(string[] args)
        {
            //组合模式:将对象组合成树形模式结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性

            //介绍
            //    意图:将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

            //主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

            //何时使用: 1、您想表示对象的部分 - 整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

            //如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。

            //关键代码:树枝内部组合该接口,并且含有内部属性 List,里面放 Component。

            //应用实例: 1、算术表达式包括操作数、操作符和另一个操作数,其中,另一个操作符也可以是操作数、操作符和另一个操作数。 2、在 JAVA AWT 和 SWING 中,对于 Button 和 Checkbox 是树叶,Container 是树枝。

            //优点: 1、高层模块调用简单。 2、节点自由增加。

            //缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

            //使用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。

            //注意事项:定义时为具体类。
            {
                Composite root = new Composite("root");
                root.Add(new Leaf("Leaf A"));
                root.Add(new Leaf("Leaf B"));

                Composite comp = new Composite("Composite X");
                comp.Add(new Leaf("Leaf XA"));
                comp.Add(new Leaf("Leaf XB"));
                root.Add(comp);

                Composite comp2 = new Composite("Composite XY");
                comp2.Add(new Leaf("Leaf XYA"));
                comp2.Add(new Leaf("Leaf XYB"));
                comp.Add(comp2);
                root.Add(new Leaf("Leaf C"));

                Leaf leafD = new Leaf("leaf D");
                root.Add(leafD);
                root.Remove(leafD);

                root.Display(1);
            }
            {
                ConcreteCompany root = new ConcreteCompany("北京总公司");
                root.Add(new HRDepartment("总公司人力资源部"));
                root.Add(new FinanceDepartment("总公司财务部"));

                ConcreteCompany comp = new ConcreteCompany("上海华东分公司");
                comp.Add(new HRDepartment("华东分公司人力资源部"));
                comp.Add(new FinanceDepartment("华东分公司财务部"));
                root.Add(comp);

                ConcreteCompany comp1 = new ConcreteCompany("南京办事处");
                comp1.Add(new HRDepartment("南京办事处人力资源部"));
                comp1.Add(new FinanceDepartment("南京办事处财务部"));
                comp.Add(comp1);

                ConcreteCompany comp2 = new ConcreteCompany("杭州办事处");
                comp2.Add(new HRDepartment("杭州办事处人力资源部"));
                comp2.Add(new FinanceDepartment("杭州办事处财务部"));
                comp.Add(comp2);


                Console.WriteLine("\n结构图:");

                root.Display(1);

                Console.WriteLine("\n职责:");

                root.LineOfDuty();
            }
            Console.ReadLine();
        }
Exemple #12
0
        static void Main(string[] args)
        {
            IComponent <string> album = new Composite <string>("Album");
            IComponent <string> point = album;

            string[] s;

            string command;

            string parameter;

            StreamReader instream = new StreamReader("Composite.dat");

            do
            {
                string t = instream.ReadLine();

                Console.WriteLine("\t\t\t\t" + t);

                s = t.Split();

                command = s[0];

                if (s.Length > 1)
                {
                    parameter = s[1];
                }
                else
                {
                    parameter = null;
                }
                switch (command)
                {
                case  "AddSet":
                    IComponent <string> c = new Composite <string>(parameter);
                    point.Add(c);
                    point = c;
                    break;

                case "AddPhoto":
                    point.Add(new Component <string>(parameter));
                    break;

                case "Remove":
                    point = point.Remove(parameter);
                    break;

                case "Find":
                    point = album.Find(parameter);
                    break;

                case "Dysplay":
                    Console.WriteLine(album.Display(0));
                    break;

                case "Quit":
                    break;
                }
            }while (!command.Equals("Quit"));
            Console.WriteLine("Application complete");
            Console.ReadKey();
        }
Exemple #13
0
        static void Main()
        {
            // Create a tree structure
            Composite root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite comp = new Composite("Composite X");
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));

            root.Add(comp);
            root.Add(new Leaf("Leaf C"));

            // Add and remove a leaf
            Leaf leaf = new Leaf("Leaf D");
            root.Add(leaf);
            root.Remove(leaf);

            // Recursively display tree
            root.Display(1);

            // Wait for user
            Console.Read();
        }
Exemple #14
0
        static void Main(string[] args)
        {
            try
            {
                {//BasicStructure
                    //初始化一个根节点
                    Component root = new Composite("root");
                    //添加两个叶节点
                    root.Add(new Leaf("Leaf A"));
                    root.Add(new Leaf("Leaf B"));

                    //实例化一个枝节点,为枝节点添加两个叶节点,然后添加至根节点
                    Component comp = new Composite("Composite X");
                    comp.Add(new Leaf("Composite XA"));
                    comp.Add(new Leaf("Composite XB"));
                    root.Add(comp);
                    //实例化一个枝节点,为枝节点添加两个叶节点,然后添加至根节点
                    Component comp2 = new Composite("Composite Y");
                    comp2.Add(new Leaf("Composite YA"));
                    comp2.Add(new Leaf("Composite YB"));
                    root.Add(comp2);

                    root.Add(new Leaf("Leaf C"));
                    Leaf leaf = new Leaf("Leaf D");
                    root.Add(leaf);
                    root.Remove(leaf);

                    root.Display(1);
                }
                {//SituationSimulation
                    Company company = new ConcreteCompany("华夏企业北京总公司");
                    company.Add(new HRDepartment("北京总公司人力资源部"));
                    company.Add(new HRDepartment("北京总公司研发部"));

                    Company company2 = new ConcreteCompany("华夏企业深圳分公司");
                    company2.Add(new HRDepartment("深圳分公司人力资源部"));
                    company2.Add(new HRDepartment("深圳分公司研发部"));
                    company.Add(company2);

                    Company company3 = new ConcreteCompany("华夏企业深圳宝安区分公司");
                    company3.Add(new HRDepartment("深圳宝安区分公司人力资源部"));
                    company3.Add(new HRDepartment("深圳宝安区分公司研发部"));
                    company2.Add(company3);

                    Company company4 = new ConcreteCompany("华夏企业深圳龙华区分公司");
                    company4.Add(new HRDepartment("深圳龙华区分公司人力资源部"));
                    company4.Add(new HRDepartment("深圳龙华区分公司研发部"));
                    company2.Add(company4);

                    Company company5 = new ConcreteCompany("华夏企业上海分公司");
                    company5.Add(new HRDepartment("上海分公司人力资源部"));
                    company5.Add(new HRDepartment("上海分公司研发部"));
                    company.Add(company5);

                    company.Show(1);
                    Console.WriteLine("职责:");
                    company.LineOfDuty();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }