Esempio n. 1
0
        static void Main(string[] args)
        {
            Client client = new Client();

            // This way the client code can support the simple leaf
            // components...
            Leaf leaf = new Leaf();

            Console.WriteLine("Client: I get a simple component:\n");
            client.ClientCode(leaf);

            // ...as well as the complex composites.
            Composite tree    = new Composite();
            Composite branch1 = new Composite();

            branch1.Add(new Leaf());
            branch1.Add(new Leaf());
            Composite branch2 = new Composite();

            branch2.Add(new Leaf());
            tree.Add(branch1);
            tree.Add(branch2);
            Console.WriteLine("Client: Now I've got a composite tree:\n");
            client.ClientCode(tree);

            Console.Write("Client: I don't need to check the components classes even when managing the tree:\n");
            client.ClientCode2(tree, leaf);


            Console.WriteLine("Price Calc:\n");
            ProductClient productClient = new ProductClient();


            Product product = new Product(10);

            Console.WriteLine("Client: I get a simple component:\n");
            productClient.ClientCode(product);

            // ...as well as the complex composites.
            Box box      = new Box();
            Box envelop1 = new Box();

            envelop1.Add(new Product(20));
            envelop1.Add(new Product(30));
            Box envelop2 = new Box();

            envelop2.Add(new Product(50));
            box.Add(envelop1);
            box.Add(envelop2);
            Console.WriteLine("Client: Now I've got a composite tree:\n");
            productClient.ClientCode(box);

            Console.Write("Client: I don't need to check the components classes even when managing the tree:\n");
            productClient.ClientCode2(box, product);
        }
Esempio n. 2
0
        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);
        }
        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();
        }
Esempio n. 4
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();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Composite root  = new Composite("root");
            Composite root1 = new Composite("A");

            root1.Add(new Leaf("-A-1"));
            root1.Add(new Leaf("-A-2"));
            Composite root2 = new Composite("B");

            root2.Add(new Leaf("-B-1"));
            root2.Add(new Leaf("-B-2"));
            root.Add(root1);
            root.Add(root2);
            root.Operation();
            Console.ReadKey();
        }
Esempio n. 6
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();
        }
Esempio n. 7
0
 private void CreateFeildLoadCarrier()
 {
     for (int i = 0; i < 8; i++)
     {
         bandolier.Add(magazine);
     }
     feildLoadCarrier.Add(bandolier);
     feildLoadCarrier.Add(multitool);
 }
Esempio n. 8
0
        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)
        {
            var leaf = new Leaf();

            var mainTree = new Composite("main tree");
            var subTree1 = new Composite("sub tree 1");

            subTree1.Add(new Leaf());
            subTree1.Add(new Leaf());

            var subTree2 = new Composite("sub tree 2");

            subTree2.Add(new Leaf());

            mainTree.Add(subTree1);
            mainTree.Add(subTree2);

            mainTree.DoOperation();
        }
Esempio n. 10
0
        private void CreateArmyRuckSack()
        {
            CreateMRE();

            armyRuckSack.Add(paracord);
            armyRuckSack.Add(pancho);
            for (int i = 0; i < 6; i++)
            {
                armyRuckSack.Add(tentStake);
            }
            armyRuckSack.Add(acu);
            armyRuckSack.Add(undergarments);
            armyRuckSack.Add(combatBoots);
            armyRuckSack.Add(mre);
            armyRuckSack.Add(mre);
        }
Esempio n. 11
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();
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            var root            = new Composite("Classification of Animals");
            var invertebrates   = new Composite("+ Invertebrates");
            var vertebrates     = new Composite("+ Vertebrates");
            var warmBlooded     = new Leaf("-- Warm-Blooded");
            var coldBlooded     = new Leaf("-- Cold-Blooded");
            var withJointedLegs = new Leaf("-- With Jointed-Legs");
            var withoutLegs     = new Leaf("-- Without Legs");

            invertebrates.Add(withJointedLegs);
            invertebrates.Add(withoutLegs);

            vertebrates.Add(warmBlooded);
            vertebrates.Add(coldBlooded);

            root.Add(invertebrates);
            root.Add(vertebrates);
            root.PrintName();
        }
Esempio n. 13
0
        private void CreateArmyLoadout()
        {
            CreateArmyHelmet();
            CreateM4A1();
            CreateArmyRuckSack();
            CreateFeildLoadCarrier();

            armyLoadout.Add(armyHelmet);
            armyLoadout.Add(m4a1);
            armyLoadout.Add(armyRuckSack);
            armyLoadout.Add(feildLoadCarrier);
            armyLoadout.Add(acu);
            armyLoadout.Add(undergarments);
            armyLoadout.Add(combatBoots);
            armyLoadout.Add(multitool);
            armyLoadout.Add(camelback);
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
 private void CreateLongHikeLoadout()
 {
     CreateCivyRucksack();
     longHikeLoadout.Add(undergarments);
     longHikeLoadout.Add(pants);
     longHikeLoadout.Add(shirt);
     longHikeLoadout.Add(hat);
     longHikeLoadout.Add(combatBoots);
     longHikeLoadout.Add(civyRuckSack);
     longHikeLoadout.Add(multitool);
 }
Esempio n. 16
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);
        }
Esempio n. 17
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();
        }
Esempio n. 18
0
 private void CreateShortHikeLoadout()
 {
     CreateBackpack();
     shortHikeLoadout.Add(backpack);
     shortHikeLoadout.Add(undergarments);
     shortHikeLoadout.Add(pants);
     shortHikeLoadout.Add(shirt);
     shortHikeLoadout.Add(combatBoots);
     shortHikeLoadout.Add(multitool);
 }
        public void GenerateOfficeBuilding()
        {
            for (int i = 1; i < 4; i++)
            {
                Composite floor = new Composite(i, "floor");

                for (int j = 1; j < 4; j++)
                {
                    Leaf newOffice = MakeOffice(j, i);

                    floor.Add(newOffice);
                }

                OfficeBuilding.Add(floor);
            }
        }
Esempio n. 20
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();
        }
Esempio n. 21
0
 private void CreateCamelback()
 {
     camelback.Add(water);
 }
Esempio n. 22
0
 private void CreateMagazine()
 {
     magazine.Add(ammo);
 }
Esempio n. 23
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();
        }
Esempio n. 24
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();
        }
Esempio n. 25
0
 private void CreateCivyRucksack()
 {
     civyRuckSack.Add(mre);
     civyRuckSack.Add(mre);
     civyRuckSack.Add(camelback);
     civyRuckSack.Add(pancho);
     civyRuckSack.Add(paracord);
     for (int i = 0; i < 6; i++)
     {
         civyRuckSack.Add(tentStake);
     }
     civyRuckSack.Add(combatBoots);
     civyRuckSack.Add(pants);
     civyRuckSack.Add(shirt);
     civyRuckSack.Add(undergarments);
 }
Esempio n. 26
0
 private void CreateBackpack()
 {
     backpack.Add(camelback);
     backpack.Add(survivalBar);
 }