主讲:李建忠
来源:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/consyscourse/CsharpOOD.aspx
Compositepublic interface IBox { public abstract void Process(); public abstract void Add(IBox box); public abstract void Remove(IBox box); } public class SingleBox:IBox { public void Add(IBox box) { throw new UnSupportedException(); } public void Remove(IBox box) { throw new UnSupportedException(); } public void Process() { //Do process for myself //... } } public class ContainerBox:IBox { ArrayList list=null; public void Add() { throw new UnSupportedException(); } public abstract void Remove() { throw new UnSupportedException(); } public void Process() { //Do process for myself //... //Do process for the box in the list if(list!=null) { foreach(IBox box in list) //内部实现递归 { box.Process(); } } } } //客户代码 class App { public static void Main() { IBox box=Factory.CreateBox(); box.Process(); } }