コード例 #1
0
	// 
	void UnitTest () {

		// 根節點
		IComponent theRoot = new Composite("Root");
		// 加入兩個最終節點
		theRoot.Add ( new Leaf("Leaf1"));
		theRoot.Add ( new Leaf("Leaf2"));

		// 子節點1
		IComponent theChild1 = new Composite("Child1");
		// 加入兩個最終節點
		theChild1.Add ( new Leaf("Child1.Leaf1"));
		theChild1.Add ( new Leaf("Child1.Leaf2"));
		theRoot.Add (theChild1);

		// 子節點2
		// 加入3個最終節點
		IComponent theChild2 = new Composite("Child2");
		theChild2.Add ( new Leaf("Child2.Leaf1"));
		theChild2.Add ( new Leaf("Child2.Leaf2"));
		theChild2.Add ( new Leaf("Child2.Leaf3"));
		theRoot.Add (theChild2);

		// 顯示
		theRoot.Operation();	


	}
コード例 #2
0
    private 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();
    }
コード例 #3
0
    /// Entry point into console application.
    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 comp1 = new Composite("Composite X");
        comp1.Add(new Leaf("Leaf XA"));
        comp1.Add(new Leaf("Leaf XB"));
        root.Add(comp1);

        Composite comp2 = new Composite("Composite Y");
        comp2.Add(new Leaf("Leaf YA"));
        comp2.Add(new Leaf("Leaf YB"));
        root.Add(comp2);

        Composite comp3 = new Composite("Composite Z");
        comp3.Add(new Leaf("Leaf ZA"));
        comp3.Add(new Leaf("Leaf ZB"));
        comp2.Add(comp3);

        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();
    }
コード例 #4
0
ファイル: Test.cs プロジェクト: onurak/GOFDesignPatterns
        public static void Test()
        {
            var root = new Composite<Component> { Node = new Component("Component A") };

            root.Add(new Component("Component A - 1"));
            root.Add(new Component("Component A - 2"));
            root.Add(new Component("Component A - 3"));
        }
コード例 #5
0
        private static Client LoadSystem()
        {
            Composite Tree     = new Composite("Tree", "İt keeps All segments");
            Composite Web      = new Composite("WebSegment", "İt keeps web_Worker");
            Composite Cyber    = new Composite("CyberSegment", "İt keeps cyber_Worker");
            Composite AI       = new Composite("AiSegment", "İt keeps AI_Worker");
            worker    worker1  = new worker("Mustafa", "Clouder", 5000);
            worker    worker2  = new worker("Ali", "Clouder", 5000);
            worker    worker3  = new worker("Veli", "Clouder", 5000);
            worker    worker4  = new worker("Emre", "AIworker", 5000);
            worker    worker5  = new worker("Veli", "AIworker", 5000);
            worker    worker6  = new worker("Kazım", "AIworker", 5000);
            worker    worker7  = new worker("Ali", "AIworker", 5000);
            worker    worker8  = new worker("veli", "Manager", 5000);
            worker    worker9  = new worker("Zeynep", "Manager", 5000);
            worker    worker10 = new worker("ŞtandartKazım", "SoftwareEn", 5000);
            worker    worker11 = new worker("Memati", "SoftwareEn", 5000);
            worker    worker12 = new worker("Cahit", "SoftwareEn", 5000);
            worker    worker13 = new worker("Mehmet", "SoftwareEn", 5000);
            worker    worker14 = new worker("Zeynep", "SoftwareEn", 5000);
            worker    worker15 = new worker("Celil", "AIworker", 5000);
            worker    worker16 = new worker("Polat", "Clouder", 5000);
            Composite AISub    = new Composite("AISub", "SubAreaOfAI");
            worker    denemeai = new worker("AliSub", "Manager", 50);

            AI.Add(AISub);
            Tree.Add(Web);
            Tree.Add(Cyber);
            Tree.Add(AI);
            AISub.Add(denemeai);
            Web.Add(worker16);
            Web.Add(worker15);
            Web.Add(worker12);
            Web.Add(worker10);
            Cyber.Add(worker14);
            Cyber.Add(worker13);
            Cyber.Add(worker12);
            Cyber.Add(worker10);
            Cyber.Add(worker8);
            Cyber.Add(worker5);
            AI.Add(worker1);
            AI.Add(worker2);
            AI.Add(worker4);
            AI.Add(worker5);
            AI.Add(worker6);
            AI.Add(worker11);
            AI.Add(worker7);
            AI.Add(worker9);
            Cyber.Add(worker3);
            Cyber.Add(worker4);
            Client client = new Client(Tree);

            return(client);
        }
コード例 #6
0
        public void SimpleTest()
        {
            Composite composite = new Composite();

            composite.Add(new Leaf());
            composite.Add(new Leaf());

            Composite childComposite = new Composite();

            composite.Add(childComposite);
            childComposite.Add(new Leaf());
        }
コード例 #7
0
    public static void Main(string[] args)
    {
        Composite composite = new Composite();

        composite.Add(new Leaf("Leaf A"));
        composite.Add(new Leaf("Leaf B"));
        composite.Add(new Leaf("Leaf C"));
        composite.Add(new Leaf("Leaf D"));

        IComponent component = composite;

        component.Operation();
    }
コード例 #8
0
        public void Composite_CompositeWithLeafs_MustBeAbleToAccessLeafs()
        {
            var composite = new Composite("mom");
            var girl      = new Leaf("girl");
            var boy       = new Leaf("boy");

            composite.Add(girl);
            composite.Add(boy);


            Assert.Contains("boy", composite.ToString());
            Assert.Contains("girl", composite.ToString());
        }
コード例 #9
0
        private Core()
        {
            // Инициализация скомпанованого списка методов многомерного линейного поиска
            Composite MDS = new Composite("Методы линейного поиска");

            MDS.Add(new Swenn());
            MDS.Add(new GoldenSection1());
            MDS.Add(new Powell());
            MDS.Add(new Fibonacci2());
            MDS.Add(new Bolzano());
            MDS.Add(new CubicInterpolation());
            MDS.Add(new Dichotomy());
            MDS.Add(new Davidon());

            // Инициализация скомпанованого списка методов многомерного градиентного поиска
            Composite MGD = new Composite("Методы многомерного поиска");

            MGD.Add(new Koshi());
            MGD.Add(new GaussSeidel());
            MGD.Add(new MethodСonjugateGradient());
            MGD.Add(new Partan3());
            MGD.Add(new NewtonGeneralizedMethod());
            MGD.Add(new MethodWithVariableMetric());
            MGD.Add(new HookeJeeves());
            MGD.Add(new Powell3());

            Methods = new Composite("Методы");
            Methods.Add(MDS);
            Methods.Add(MGD);
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: striveCj/StudyCode
        public static void CompositePattern()
        {
            Composite root = new Composite("root");

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

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

            root.Add(new Leaf("Leaf xa"));
            root.Add(new Leaf("Leaf xb"));

            root.Add(comp);
            Composite comp2 = new Composite("Composite X");

            comp2.Add(new Leaf("Leaf xya"));
            comp2.Add(new Leaf("Leaf xyb"));
            comp.Add(comp2);

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

            Leaf leaf = new Leaf("Leaf D");

            root.Add(leaf);
            root.Remove(leaf);
            root.Display(1);
            Console.Read();
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: michailnk/core-Net
        static void Main(string[] args)
        {
            Component root    = new Composite("root");
            Component branch1 = new Composite("branch1");
            Component branch2 = new Composite("branch2");
            Component leaf1   = new Leaf("L1");
            Component leaf2   = new Leaf("L2");

            root.Add(branch1);
            root.Add(branch2);
            branch1.Add(leaf1);
            branch2.Add(leaf2);
            root.Operation();
        }
コード例 #12
0
    static void Main()
    {
        Component root = new Composite("root");

        root.Add(new Leaf("Leaf A"));
        root.Add(new Leaf("Leaf B"));
        Composite comp = new Composite("Comp C");

        comp.Add(new Leaf("Leaf CA"));
        comp.Add(new Leaf("Leaf CB"));
        root.Add(comp);
        root.Display(1);
        Console.ReadKey();
    }
コード例 #13
0
ファイル: Program.cs プロジェクト: m1c1b/GOF-Patterns
        static void Main()
        {
            Component root    = new Composite("root");
            Component branch1 = new Composite("br1");
            Component branch2 = new Composite("br2");
            Component leaf1   = new Composite("leaf1");
            Component leaf2   = new Composite("leaf2");

            root.Add(branch1);
            root.Add(branch2);
            branch1.Add(leaf1);
            branch2.Add(leaf2);

            root.Operation();
        }
コード例 #14
0
        public void Check_Composite()
        {
            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.Display(1);
        }
コード例 #15
0
        private static bool StartSeries(ListType listType)
        {
            var processed = listType == ListType.ContainerList
                ? " AND series.isUnprocessed IS NULL "
                : listType == ListType.PreliminaryInventory
                    ? " AND series.isUnprocessed = 1 "
                    : " ";
            var seriesList = _db.Fetch <SeriesModel>("SELECT collection.eadID, series.id, series.number, series.title, series.description " +
                                                     "FROM series INNER JOIN collection ON collection.id = series.collectionID " +
                                                     "WHERE collection.eadID = @0" + processed + "ORDER BY CAST(REPLACE(number, '-', '.') AS FLOAT) ASC", _eadId);

            if (!seriesList.Any())
            {
                return(false);
            }

            var depth    = 1;
            var position = 1;

            foreach (var series in seriesList)
            {
                var comp = new Series("series", series);
                comp.Depth    = depth;
                comp.Position = position;
                var std             = Utils.GetTitleDate(series.title, series.number, "Series");
                var structuralTitle = "Series " + series.number;
                comp.isArchivalObject  = std.title != structuralTitle;
                series.SeriesTitleDate = std;
                series.number          = series.number.Trim();
                //var started = false;
                var started = GetSubSeriesByID("seriesID", series.id, comp);
                if (!started)
                {
                    started = GetBoxByID("seriesID", series.id, comp);
                }
                //if (!started) started = GetItemByID("seriesID", series.id, comp);
                if (!started)
                {
                    started = GetFolderByID("seriesID", series.id, comp);
                }
                //if (!started) started = getItemByID("seriesID", dv[i][1].ToString(), c01);

                _root.Add(comp);
                position++;
            }

            return(true);
        }
コード例 #16
0
        static void Main()
        {
            Composite district = new Composite("KPI-1");                         //streets
            Composite street1  = new Composite("Metallistov street");

            district.Add(street1);
            Composite street2 = new Composite("Kovalsky lane");

            district.Add(street2);             //buildings
            Composite build1 = new Composite("build#10");
            Composite build2 = new Composite("build#12");
            Composite build3 = new Composite("build#5");

            street1.Add(build1);
            street1.Add(build2);
            street2.Add(build3);
            Flat fl1 = new Flat("#1");
            Flat fl2 = new Flat("#1");
            Flat fl3 = new Flat("#2");
            Flat fl4 = new Flat("#1");

            build1.Add(fl1);
            build2.Add(fl2);
            build2.Add(fl3);
            build3.Add(fl4);
            district.GetElectroInfo();
            district.Display(2);
            Console.ReadKey();
        }
コード例 #17
0
 //------------------------------------------------------------------
 public void Brake(Composite action, int times = 1)
 {
     action.Add(new Repeated(Car.Brake, times)
     {
         Name = "Brake"
     });
 }
コード例 #18
0
    protected override void Start()
    {
        base.Start();

        m_bb = new Blackboard();
        m_bb.SetStats(m_stats);
        m_bt   = new BehaviorTree();
        m_root = new Selector();

        m_bt.SetRoot(m_root);

        m_flee = (Composite)m_root.Add(new Sequence());
        m_flee.Add(new IsHpLow());
        m_flee.Add(new SetRandomLocation());
        m_flee.Add(new MoveToPosition());

        m_attack = (Composite)m_root.Add(new Sequence());
        m_attack.Add(new IsPlayerInRange());
        m_attack.Add(new TargetPlayer());

        m_moveToPlayer = (Composite)m_root.Add(new Sequence());
        m_moveToPlayer.Add(new IsPlayerInSight());
        m_moveToPlayer.Add(new TargetPlayer());
        m_moveToPlayer.Add(new MoveToPosition());

        m_walk = (Composite)m_root.Add(new Sequence());
        m_walk.Add(new StandOrWalk());
        m_walk.Add(new SetRandomLocation());
        m_walk.Add(new MoveToPosition());

        m_idle = (Composite)m_root.Add(new Sequence());
        m_idle.Add(new Idle());
    }
コード例 #19
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:");
            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:");
            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);
        }
コード例 #20
0
        static void Main(string[] args)
        {
            Client client = new Client();


            Simple leaf = new Simple();

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


            Composite tree    = new Composite();
            Composite branch1 = new Composite();

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

            branch2.Add(new Simple());
            tree.Add(branch1);
            tree.Add(branch2);
            Console.WriteLine("Client: Now I've got a composite tree:");
            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);
        }
コード例 #21
0
 private void LoadAdapters()
 {
     foreach (var login in Logins)
     {
         Composite.Add(LoginFactory.CreateInstance(login));
     }
 }
コード例 #22
0
        private void HandlerEventCreateMethod(object sender)
        {
            FormCreateMethod form = sender as FormCreateMethod;

            if (form != null && form.Method != null)
            {
                form.Method.Lim = form.LimParams;
                form.Method.NormalizationDirections = form.Norma;

                if (form.NumberUsedComposite != -1)
                {
                    EmptyMethod usedMethods = EventGetUsedMethods?.Invoke(form.NumberUsedComposite);
                    if (usedMethods != null)
                    {
                        form.Method.MethodsUsed = usedMethods;
                    }
                }

                composite.Add(form.Method);
                dataGridView.Rows.Add(new object[] { form.Method.Name,
                                                     form.LimParams?.ToString(),
                                                     form.Norma,
                                                     form.NumberUsedComposite != -1 ? form.NumberUsedComposite.ToString() : "" });
            }
        }
コード例 #23
0
        private void DemoComposite()
        {
            // some leafs
            LeafBox   b1    = new LeafBox(100);
            Composite comp1 = new Composite();

            comp1.Add(b1);
            comp1.Add(new LeafBox(50));

            Composite comp2 = new Composite();

            comp2.Add(b1);
            comp2.Add(comp1);

            Console.WriteLine(comp2.TotalWeight());
        }
コード例 #24
0
        public void Composite()
        {
            IComponent <string> root    = new Composite <string>("root");
            IComponent <string> item1   = new Composite <string>("item1");
            IComponent <string> item11  = new Composite <string>("item1.1");
            IComponent <string> item111 = new Component <string>("item1.1.1");
            IComponent <string> item112 = new Component <string>("item1.1.2");
            IComponent <string> item12  = new Composite <string>("item1.2");
            IComponent <string> item2   = new Composite <string>("item2");
            IComponent <string> item21  = new Component <string>("item2.1");
            IComponent <string> item3   = new Composite <string>("item3");
            IComponent <string> item31  = new Component <string>("item3.1");

            //set item1
            item11.Add(item111);
            item11.Add(item112);
            item1.Add(item11);
            item1.Add(item12);
            root.Add(item1);

            //set item2
            item2.Add(item21);
            root.Add(item2);

            //set item3
            item3.Add(item31);
            root.Add(item3);

            var result = root.Display(0);

            Assert.AreEqual(result, "root: 3 items in list\n--item1: 2 items in list\n----item1.1: 2 items in list\n------item1.1.1\n------item1.1.2\n----item1.2: 0 items in list\n--item2: 1 items in list\n----item2.1\n--item3: 1 items in list\n----item3.1\n");
        }
コード例 #25
0
        static void Main(string[] args)
        {
            Component root    = new Composite("root");
            Component branch1 = new Composite("br1");
            Component branch2 = new Composite("br2");
            Component leaf1   = new Leaf("leaf1");
            Component leaf2   = new Leaf("leaf2");
            Component leaf3   = new Leaf("leaf3");

            //Create tree

            root.Add(branch1);
            root.Add(branch2);

            //           *           //Root
            //        /     \
            //       *       *       //branch1 and branch2

            branch1.Add(leaf1);
            branch2.Add(leaf2);
            branch1.Add(leaf3);

            //           *           //Root
            //        /     \ 
            //       *       *       //branch1 and branch2
            //     / |        \
            //    ~  ~         ~     // leaf1,leaf3 and leaf2
            //
            //
            root.Operation();        // Creating
        }
コード例 #26
0
        public void TestCompositeObject()
        {
            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:");
            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:");
            client.ClientCode(tree);

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

            Assert.NotNull(resutl);
            Assert.Equal(resutl, "Branch(Branch(Leaf+Leaf)+Branch(Leaf)+Leaf)");
        }
コード例 #27
0
ファイル: Program.cs プロジェクト: PresnyakovIgorDev/Patterns
        private static void CompositeTest()
        {
            Console.WriteLine("---------------------------");
            Console.WriteLine("CompositeTest");
            Component box = new Composite("Коробка", 1);

            box.Add(new Leaf("Книга", 50));
            var subBox = new Composite("Контейнер", 2);

            subBox.Add(new Leaf("Видеокарта", 100));
            subBox.Add(new Leaf("CPU", 150));

            box.Add(subBox);
            box.Display();
            Console.WriteLine(box.GetFullCost());
            Console.WriteLine("---------------------------");
        }
コード例 #28
0
        private void btnTest2_Click(object sender, EventArgs e)
        {
            Line      line      = new Line(new Point(20, 0), new Point(100, 0));
            Composite composite = new Composite();

            composite.Add(line);

            for (int k = 10; k <= 270; k += 10)
            {
                composite.Add(line.Duplicate().Rotate(k));
            }

            DrawImage image = new DrawImage(this.pictureBox1.Width, this.pictureBox1.Height);

            composite.Draw(image);
            this.pictureBox1.Image = image.Image;
        }
コード例 #29
0
        public void CompositePattern_UseComposite()
        {
            var root = new Composite("Root");

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

            var 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"));

            root.Display(1);
        }
コード例 #30
0
        static void Main(string[] args)
        {
            //facade
            DataManager   dataManager   = new DataManager();
            ReviewManager reviewManager = new ReviewManager();
            RatingFacade  ratingPortal  = new RatingFacade(dataManager, reviewManager);

            ratingPortal.Start();


            //proxy
            List <string> resourses = new() { "EBEC", "Chess tournament", "WIPZ KR" };
            UserRequest   request   = new UserRequest();

            System.Console.WriteLine("\nSimulating work for unregistered User");
            foreach (var res in resourses)
            {
                RealResource realResource = new RealResource(res);
                Proxy        proxy        = new Proxy(realResource, 0); //unregistered user, wirh accessLevel = 0
                request.Get(proxy);
            }
            System.Console.WriteLine("\nSimulating work for registered User");
            foreach (var res in resourses)
            {
                RealResource realResource = new RealResource(res);
                Proxy        proxy        = new Proxy(realResource, 1); //unregistered user, wirh accessLevel = 0
                request.Get(proxy);
            }


            //composite
            //student requesting info for PZ
            Client client = new Client();

            //PZ construction
            System.Console.WriteLine("\n\nInfo about PZ\n");
            Composite PZ = new Composite();

            Composite Title = new Composite();

            Title.Add(new Leaf("Title: Prog Zabezp"));
            Composite Rating = new Composite();

            Rating.Add(new Leaf("Rating: 10/12"));
            Composite Content = new Composite();

            Content.Add(new Leaf("Content: Nice stuff"));
            Composite Comments = new Composite();

            Comments.Add(new Leaf("Comments: How about Levus?"));
            PZ.Add(new List <Component> {
                Title, Rating, Content, Comments
            });
            //client.GetContent(PZ);
            client.AddNewComponent(Comments, new Leaf("\tComment: not well..."));
            client.GetContent(PZ);
        }
    }
コード例 #31
0
        private static void ShowComposite()
        {
            Console.WriteLine("================================================");
            Console.WriteLine("Pattern code (Composite):");
            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"));

            Leaf leaf = new Leaf("Leaf D");

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

            root.Display(1);


            Console.WriteLine("\nReal code (Composite):");
            CompositeElement picture = new CompositeElement("Picture");

            picture.Add(new PrimitiveElement("Red Line"));
            picture.Add(new PrimitiveElement("Blue Circle"));
            picture.Add(new PrimitiveElement("Green Box"));

            CompositeElement circles = new CompositeElement("Two Circles");

            circles.Add(new PrimitiveElement("Black Circle"));
            circles.Add(new PrimitiveElement("White Circle"));
            picture.Add(circles);

            PrimitiveElement line = new PrimitiveElement("Yellow Line");

            picture.Add(line);
            picture.Remove(line);

            picture.Display(1);
        }
コード例 #32
0
        static void CompositeTester()
        {
            #region sample 1
            var root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            var 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
            var leaf = new Leaf("Leaf D");
            root.Add(leaf);
            root.Remove(leaf);

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

            #region sample 2
            // Create a tree structure
            var rootImpl = new CompositeElement("Picture");
            rootImpl.Add(new PrimitiveElement("Red Line"));
            rootImpl.Add(new PrimitiveElement("Blue Circle"));
            rootImpl.Add(new PrimitiveElement("Green Box"));

            // Create a branch
            var compImpl = new CompositeElement("Two Circles");
            compImpl.Add(new PrimitiveElement("Black Circle"));
            compImpl.Add(new PrimitiveElement("White Circle"));
            rootImpl.Add(compImpl);

            // Add and remove a PrimitiveElement
            var pe = new PrimitiveElement("Yellow Line");
            rootImpl.Add(pe);
            rootImpl.Remove(pe);

            // Recursively display nodes
            rootImpl.Display(1);
            #endregion
        }
コード例 #33
0
        private static void Do()
        {
            var composite = new Composite();
            var component = new Component();

            component.Operation();
            composite.Add(component);
            composite.Operation();
        }
コード例 #34
0
        /// <summary>
        /// 组合模式.
        /// </summary>
        private static void Composite()
        {
            var root    = new Composite("root");
            var net     = new Composite("net");
            var program = new Composite("program");

            root.Add(net);
            root.Add(program);
            var c    = new Composite("c#");
            var core = new Composite("core");

            net.Add(c);
            net.Add(core);
            var java = new Composite("java");

            program.Add(java);
            root.Display(1);
        }
コード例 #35
0
        public static void Demo()
        {
            // 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);
        }
コード例 #36
0
  public 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 l = new Leaf( "Leaf D" );
    root.Add( l );
    root.Remove( l );

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

  }
コード例 #37
0
            public static void Composite()
            {
                IComponent root = new Composite("root");
                root.Add(new Leaf("Leaf A"));
                root.Add(new Leaf("Leaf B"));
                root.Add(new Leaf("Leaf C"));
                Composite comp = new Composite("Composite X");
                comp.Add(new Leaf("Leaf XA"));
                comp.Add(new Leaf("Leaf XB"));
                comp.Add(new Leaf("Leaf XC"));

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

                root.Display(1);

            }