笔记-大话设计模式-19 组合模式
2015-09-09 00:31
369 查看
组合模式(Composite),将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
Demo1:
Test:
Demo2:
Test:
Demo1:
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); }
class Composite : Component { private IList<Component> children = new List<Component>(); public Composite(string name) : base(name) { } public override void Add(Component c) { children.Add(c); } public override void Remove(Component c) { children.Remove(c); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); foreach (var component in children) { component.Display(depth + 2); } } }
class Leaf : Component { public Leaf(string name) : base(name) { } public override void Add(Component c) { throw new NotImplementedException(); } public override void Remove(Component c) { throw new NotImplementedException(); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } }
Test:
Composite root = new Composite("root"); root.Add(new Leaf("Leaf A")); root.Add(new Leaf("Leaf B")); Composite firstLevel = new Composite("第一级"); firstLevel.Add(new Leaf("第一级的叶子")); root.Add(firstLevel); Composite secondLevel = new Composite("第二级"); secondLevel.Add(new Leaf("第二级的叶子")); firstLevel.Add(secondLevel); root.Display(1);
Demo2:
abstract class Company { protected string name; public Company(string name) { this.name = name; } public abstract void Add(Company c); public abstract void Remove(Company c); public abstract void Display(int depth); public abstract void Dutys(); }
class ConcreteCompany : Company { private IList<Company> children = new List<Company>(); public ConcreteCompany(string name) : base(name) { } public override void Add(Company c) { children.Add(c); } public override void Remove(Company c) { children.Remove(c); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); foreach (var item in children) { item.Display(depth + 2); } } public override void Dutys() { foreach (var item in children) { item.Dutys(); } } }
class ADepartment : Company { public ADepartment(string name) : base(name) { } public override void Add(Company c) { throw new NotImplementedException(); } public override void Remove(Company c) { throw new NotImplementedException(); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void Dutys() { Console.WriteLine("{0} 的ADepartment履行职责", name); } }
class BDepartment : Company { public BDepartment(string name) : base(name) { } public override void Add(Company c) { throw new NotImplementedException(); } public override void Remove(Company c) { throw new NotImplementedException(); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void Dutys() { Console.WriteLine("{0} 的BDepartment履行职责", name); } }
Test:
ConcreteCompany root = new ConcreteCompany("北京总公司"); root.Add(new ADepartment("总公司A部门")); root.Add(new BDepartment("总公司B部门")); ConcreteCompany branch1 = new ConcreteCompany("华东分公司"); branch1.Add(new ADepartment("分公司的A部门")); branch1.Add(new BDepartment("分公司的B部门")); root.Add(branch1); root.Display(1); Console.WriteLine(Environment.NewLine); root.Dutys();
相关文章推荐
- jquery 简单实用的Tab切换
- windows下mysql5.1忘记root密码解决方法[win7]
- 加速Android Studio/Gradle构建
- hashCode
- Linq 杂记(读书笔记)
- Android采用ListView实现数据列表显示1-使用SimpleCursorAdapter进行数据绑定
- unity3d插件
- Unity 模型的制作规范
- WCF服务开发与调用的完整示例
- Centos 安装 Moosefs文件系统
- 关于sed中的Pattern Space和Hold Space的随笔
- vim的正则表达式
- Unity中的网络编程
- ZOJ 1093 Monkey and Banana
- uva 1213 不同素数之和
- 工作中感受到的消息中间件在分布式系统中的使用场景
- mysql函数之——控制流函数
- Ibatis和myIbatis 返回插入的主键ID
- zookeeper集群安装配置
- poj 2464(区间更新)