最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 組合模式

 更新時間:2012年10月29日 14:02:53   作者:  
組合模式:將對象組合成樹形結(jié)構(gòu)以表示‘部分-整體’的層次結(jié)構(gòu)。組合模式使得用戶對單個對象和組合對象的使用具有一致性。需求中式體現(xiàn)部分與整體層次的結(jié)構(gòu)時,統(tǒng)一地使用組合對象中的所有對象時,應(yīng)該考慮使用組合模式
結(jié)構(gòu)圖:

抽象對象:
復(fù)制代碼 代碼如下:

    abstract class Component
    {
        protected string name;
        public Component(string name)
        {
            this.name = name;
        }
        public abstract void Add(Component c);
        public abstract void Remove(Component c);
        public abstract void Display(int depth);
    }

無子節(jié)點的:
復(fù)制代碼 代碼如下:

    class Leaf : Component
    {
        public Leaf(string name)
            : base(name)
        { }
        public override void Add(Component c)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Cannot add to a Leaf");
        }
        public override void Remove(Component c)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Cannot remove to a Leaf");
        }
        public override void Display(int depth)
        {
            //throw new NotImplementedException();
            Console.WriteLine(new string('-', depth) + name);
        }
    }

可以有子結(jié)點:
復(fù)制代碼 代碼如下:

    class Composite : Component
    {
        private List<Component> children = new List<Component>();
        public Composite(string name)
            : base(name)
        { }
        public override void Add(Component c)
        {
            //throw new NotImplementedException();
            children.Add(c);
        }
        public override void Remove(Component c)
        {
            //throw new NotImplementedException();
            children.Remove(c);
        }
        public override void Display(int depth)
        {
            //throw new NotImplementedException();
            Console.WriteLine(new string('-', depth) + name);
            foreach (Component component in children)
            {
                component.Display(depth + 2);
            }
        }
    }

 主函數(shù)調(diào)用:
復(fù)制代碼 代碼如下:

    class Program
    {
        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);
            Composite comp2 = new Composite("Composite X");
            comp2.Add(new Leaf("Leaf XYA"));
            comp2.Add(new Leaf("Leaf XYB"));
            comp.Add(comp2);
            root.Display(1);
            Console.ReadKey();
        }
    }
 

相關(guān)文章

最新評論

柘荣县| 福鼎市| 凭祥市| 扎兰屯市| 平邑县| 武陟县| 灵川县| 新丰县| 许昌市| 肥东县| 泰宁县| 镇平县| 文成县| 大宁县| 房产| 玛沁县| 大竹县| 大冶市| 翼城县| 崇州市| 突泉县| 定结县| 荆门市| 图木舒克市| 象山县| 玛纳斯县| 离岛区| 宁化县| 油尖旺区| 比如县| 新建县| 泾阳县| 吉隆县| 台东县| 永川市| 安泽县| 宁津县| 定边县| 大埔区| 衢州市| 千阳县|