设计模式之组合模式(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
代码下载
- 设计模式之组合模式(Composite Pattern)
- 设计模式之组合模式(Composite Pattern)
- 设计模式之组合模式(Composite Pattern)
- 设计模式之组合模式---Composite Pattern
- 设计模式之组合模式(Composite Pattern)
- net设计模式实例之组合模式(Composite Pattern)
- Php设计模式之【组合模式Composite Pattern】
- 设计模式之组合模式(Composite Pattern)
- JAVA设计模式之 组合模式【Composite Pattern】
- C#设计模式之组合模式(Composite Pattern)
- 设计模式(结构型)之组合模式(Composite Pattern)
- 设计模式之----组合模式(Composite Design Pattern)
- Java设计模式之--组合模式(Composite Pattern)
- 设计模式总结之Composite Pattern(组合模式)
- Net设计模式实例之组合模式(Composite Pattern)
- 结构型设计模式之组合模式(Composite Pattern)
- 【23种设计模式】之 组合模式(Composite Pattern)
- 设计模式 - Composite Pattern(组合模式)
- OpenCV学习笔记(8)VS2008 MFC下使用OpenCV2.0进行简单图像处理
- WSN TinyOS学习
- OpenCV学习笔记(9)利用MFC的Picture控件显示图像+播放视频和捕获摄像头画面
- 最小生成树
- 最短路径
- 设计模式之组合模式(Composite Pattern)
- AccordionPane实现自动展开和收缩
- 字符串匹配之Rabin-Karp算法
- 利用java反射机制实现自动调用类的方法
- oracle view
- 2011,那些陪创业者走过的热门微博
- Struts2国际化小结(转帖)
- [HDOJ 1012] u Calculate e (简单题)
- 从众心理