设计模式学习笔记——组合模式(Composite)
1.特点:用树状结构表示“整体-部分”的层次关系,使单个对象(叶节点)与组合对象(枝节点)的使用具有一致性。
2.概念:将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
3.类图:
4.程序实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | /// <summary> /// 一个抽象构件,声明一个接口用于访问和管理Component的子部件 /// </summary> public abstract class Component { protected string name; public Component( string name) { this .name = name; } /// <summary> /// 增加一个节点 /// </summary> /// <param name="component"></param> public abstract void Add(Component component); /// <summary> /// 移除一个节点 /// </summary> /// <param name="component"></param> public abstract void Remove(Component component); /// <summary> /// 显示层级结构 /// </summary> public abstract void Display( int level); } /// <summary> /// 叶子节点 /// </summary> public class Leaf : Component { public Leaf( string name) : base (name) { } /// <summary> /// 由于叶子节点没有子节点,所以Add和Remove方法对它来说没有意义,但它继承自Component,这样做可以消除叶节点和枝节点对象在抽象层次的区别,它们具备完全一致的接口。 /// </summary> /// <param name="component"></param> public override void Add(Component component) { Console.WriteLine( "Can not add a component to a leaf." ); } /// <summary> /// 实现它没有意义,只是提供了一个一致的调用接口 /// </summary> /// <param name="component"></param> public override void Remove(Component component) { Console.WriteLine( "Can not remove a component to a leaf." ); } public override void Display( int level) { Console.WriteLine( new string ( '-' ,level) + name); } } /// <summary> /// 定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作 /// </summary> public class Composite : Component { public Composite( string name) : base (name) { } /// <summary> /// 一个子对象集合,用来存储其下属的枝节点和叶节点 /// </summary> private List<Component> children = new List<Component>(); /// <summary> /// 增加子节点 /// </summary> /// <param name="component"></param> public override void Add(Component component) { children.Add(component); } /// <summary> /// 移除子节点 /// </summary> /// <param name="component"></param> public override void Remove(Component component) { children.Remove(component); } public override void Display( int level) { Console.WriteLine( new string ( '-' , level) + name); // 遍历其子节点并显示 foreach (Component component in children) { component.Display(level+2); } } } class Program { static void Main( string [] args) { // 生成树根,并为其增加两个叶子节点 Component root = new Composite( "Root" ); root.Add( new Leaf( "Leaf A in Root" )); root.Add( new Leaf( "Leaf B in Root" )); // 为根增加两个枝节点 Component branchX = new Composite( "Branch X in Root" ); Component branchY = new Composite( "Branch Y in Root" ); root.Add(branchX); root.Add(branchY); // 为BranchX增加页节点 branchX.Add( new Leaf( "Leaf A in Branch X" )); // 为BranchX增加枝节点 Component branchZ = new Composite( "Branch Z in Branch X" ); branchX.Add(branchZ); // 为BranchY增加叶节点 branchY.Add( new Leaf( "Leaf in Branch Y" )); // 为BranchZ增加叶节点 branchZ.Add( new Leaf( "Leaf in Branch Z" )); // 显示树 root.Display(1); Console.Read(); } } |
分类:
设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构