Example #1
0
        public void Test_Composite()
        {
            int result = -1;

            MyComposite c1 = new MyComposite();

            c1.Add(new MyCompositeLeaf(1));
            c1.Add(new MyCompositeLeaf(2));

            MyComposite c2 = new MyComposite();

            c2.Add(new MyCompositeLeaf(3));
            c2.Add(new MyCompositeLeaf(1));


            MyComposite c0 = new MyComposite();

            c0.Add(c1);
            c0.Add(new MyCompositeLeaf(5));
            c0.Add(c2);

            result = c0.Operation("param0");
            Assert.AreEqual(12, result);

            // access to subchildren
            result = c0.GetChild(0).Operation("param1");
            Assert.AreEqual(3, result);


            // invalid child
            Assert.IsNull(c0.GetChild(-1));
            Assert.IsNull(c0.GetChild(5));
        }
Example #2
0
    // Use this for initialization
    void Start()
    {
        MyIComponent theRoot = new MyComposite("Root");

        theRoot.Add(new MyLeaf("叶子111"));
        theRoot.Add(new MyLeaf("叶子222"));

        MyIComponent theChild1 = new MyComposite("子节点1");

        theChild1.Add(new MyLeaf("子节点1的叶子1"));
        theChild1.Add(new MyLeaf("子节点2的叶子2"));

        theRoot.Add(theChild1);
        theRoot.Operation();

        MyLeaf myLeaf = new MyLeaf("111");

        myLeaf.Add(new MyLeaf("2222"));
        myLeaf.Operation();
    }