设计模式学习8 Composite
2010-07-21 13:40
344 查看
应用场景:
客户代码过多的依赖对象容器的复杂的内部实现,对象容器内部的变化将导致客户端的代码的剧烈变化。将客户代码和对象容器解耦,从而更能够应对变化。实现代码:
public interface IBox { void Process(); void Add(IBox box); void Remove(IBox box); } public class SingleBox : IBox { public void Process() { // do process for myself } public void Add(IBox box) { // 两个实现方案 // 1. do nothing // 2. throw exception } public void Remove(IBox box) { // 1. do nothing or // 2. throw exception } } public class ContainerBox : IBox { // do process for myself // do the process fo r the box in the list ArrayList list; public void Process() { if (list != null) { foreach (IBox box in list) { box.Process(); } } } public void Add(IBox box) { if (list == null) { list = new ArrayList(); } list.Add(box); } public void Remove (IBox box) { if (list == null) { throw new Exception(); } list.Remove(box); } public ArrayList GetBoxes() { return list; } }
相关文章推荐
- 设计模式学习—组合实体模式(Composite Entity Design Pattern)
- 设计模式学习笔记(九)——Composite组成
- 步步为营 .NET 设计模式学习笔记 十五、Composite(组合模式)
- Composite——设计模式学习笔记
- java 设计模式学习笔记八 composite组合模式
- 设计模式学习笔记-Composite
- Composite——设计模式学习笔记
- java 设计模式学习笔记八 composite组合模式
- 设计模式学习笔记---composite模式
- C#面向对象设计模式学习笔记(8) - Composite 组合模式(结构型模式)
- 设计模式学习(结构型模式)—组合模式(Composite)
- 设计模式学习笔记---组合模式composite(Java版)
- 设计模式学习笔记八——Composite模式
- 学习php设计模式 php实现合成模式(composite)
- 设计模式学习之组合模式(Composite)
- 设计模式学习笔记Composite组合模式
- 阿Sam的设计模式学习笔记---- Composite模式
- 设计模式学习8 Composite
- 【设计模式】学习笔记13:组合模式(Composite)
- 【设计模式学习笔记九】【结构型模式】【组合模式(Composite)】