设计模式之组合模式(Composite Pattern)

来源:互联网 发布:东邪西毒 鸡蛋女 知乎 编辑:程序博客网 时间:2024/05/17 18:01

设计模式之组合模式(Composite Pattern)


代码下载

1.概念
将对象组合成树形结构以表示“部分-整体”的层次结构。它使得客户对单个对象和复合对象的使用具有一致性。
纯粹利用这种模式写程序的不常见,C#中TreeView与TreeNode类已经包装的功能足够强大。TreeNode就是这种模式的典型应用。

2.类图

 Model.cs

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CompositePattern{    public class Model    {        public string ID        {            get;            set;                   }        public string Name        {            get;            set;        }    }}


AbstractComponect.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CompositePattern{    public abstract class AbstractComponect    {        public abstract void Add(AbstractComponect mm);        public abstract void Delete(AbstractComponect mm);        public abstract string GetData(int indent);        protected string _Name = "";        protected Model _Model = null;        public AbstractComponect(string name)        {            _Name = name;        }        public AbstractComponect(string name, Model Model)        {            _Name = name;            _Model = Model;        }    }}

Tree .cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CompositePattern{    public class Tree : AbstractComponect    {        private List<AbstractComponect> _list = null;        public Tree(string name)            : base(name)        {            _list = new List<AbstractComponect>();        }        public override void Add(AbstractComponect mm)        {            _list.Add(mm);        }        public override void Delete(AbstractComponect mm)        {            _list.Remove(mm);        }        public override string GetData(int indent)        {            string s = new String('—', indent) +                 "树枝名称:" + _Name + "\r\n";            foreach (AbstractComponect mmc in _list)            {                s += mmc.GetData(indent + 2);            }            return s;        }    }}


Leaf.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CompositePattern{    public class Leaf : AbstractComponect    {               public Leaf(string name, Model md)            : base(name, md)        {                    }        public override void Add(AbstractComponect mm)        {            throw new NotImplementedException();        }        public override void Delete(AbstractComponect mm)        {            throw new NotImplementedException();        }        public override string GetData(int indent)        {            return new String('—', indent) +               "树叶名称:" + _Name +               ";信息内容:" + _Model.Name + "\r\n";        }    }}


3.测试代码
 Tree root = new Tree("树枝1");
            root.Add(new Leaf("树叶1.1", new Model() { ID = "1.1", Name = "叶子1" }));
            root.Add(new Leaf("树叶1.2", new Model() { ID = "1.2", Name = "叶子2" }));

            Tree root2 = new Tree("树枝2");
            root2.Add(new Leaf("树叶2.1", new Model() { ID = "2.1", Name = "叶子3" }));
            root2.Add(new Leaf("树叶2.2", new Model() { ID = "2.2", Name = "叶子4" }));

            root.Add(root2);

            this.richTextBox1.Text = root.GetData(1);


3.运行结果

—树枝名称:树枝1
———树叶名称:树叶1.1;信息内容:叶子1
———树叶名称:树叶1.2;信息内容:叶子2
———树枝名称:树枝2
—————树叶名称:树叶2.1;信息内容:叶子3
—————树叶名称:树叶2.2;信息内容:叶子4

 

代码下载

原创粉丝点击