CSharp: Composite Pattern in donet core 3
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 | /// <summary> ///组合模式 Composite Pattern /// geovindu,Geovin Du edit /// /// </summary> interface IEmployee { /// <summary> /// To set an employee name /// 姓名 /// </summary> string Name { get ; set ; } /// <summary> /// To set an employee department /// 部门 /// </summary> string Dept { get ; set ; } /// <summary> /// To set an employee designation /// 描述 /// </summary> string Designation { get ; set ; } /// <summary> /// To display an employee details /// 清单详情 /// </summary> void DisplayDetails(); // string getToString(); } /// <summary> /// Leaf Node /// </summary> class Employee : IEmployee { /// <summary> /// 姓名: /// </summary> public string Name { get ; set ; } /// <summary> /// 部门 /// </summary> public string Dept { get ; set ; } /// <summary> /// 描述 /// </summary> public string Designation { get ; set ; } /// <summary> /// 详情清单 Details of a leaf node /// </summary> public void DisplayDetails() { // Console.WriteLine($"\t姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}"); Console.WriteLine(getToString()); } /// <summary> /// /// </summary> /// <returns></returns> public string getToString() { return $ "\t姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}" ; } } /// <summary> /// 父类 Non-leaf node /// </summary> class CompositeEmployee : IEmployee { /// <summary> /// 姓名: /// </summary> public string Name { get ; set ; } /// <summary> /// 部门 /// </summary> public string Dept { get ; set ; } /// <summary> /// 描述 /// </summary> public string Designation { get ; set ; } /// <summary> /// /// </summary> /// <returns></returns> public string getToString() { //return $"{nameof(Name)}: {Name}, {nameof(Dept)}: {Dept}, {nameof(Designation)}: {Designation}"; return $ "\t姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}" ; } /// <summary> /// 子类对象 The container for child objects /// </summary> private List<IEmployee> subordinateList = new List<IEmployee>(); /// <summary> /// 添加 To add an employee /// </summary> /// <param name="e"></param> public void AddEmployee(IEmployee e) { subordinateList.Add(e); } /// <summary> /// 移除 To remove an employee /// /// </summary> /// <param name="e"></param> public void RemoveEmployee(IEmployee e) { subordinateList.Remove(e); } /// <summary> /// 详情清单 Details of a composite node /// </summary> public void DisplayDetails() { //getToString(); //Console.WriteLine($"\n姓名:{Name} 工作在 {Dept} 部门.描述:{Designation}"); Console.WriteLine(getToString()); foreach (IEmployee e in subordinateList) { e.DisplayDetails(); } } } |
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 | /// <summary> ///组合模式 Composite Pattern /// geovindu,Geovin Du edit /// </summary> public class GraphicObject { /// <summary> /// /// </summary> public virtual string Name { get ; set ; } = "组" ; /// <summary> /// /// </summary> public string Color; /// <summary> /// /// </summary> private readonly Lazy<List<GraphicObject>> children = new Lazy<List<GraphicObject>>(); /// <summary> /// /// </summary> public List<GraphicObject> Children => children.Value; /// <summary> /// /// </summary> /// <param name="sb"></param> /// <param name="depth"></param> private void Print(StringBuilder sb, int depth) { sb.Append( new string ( '*' , depth)) .Append( string .IsNullOrWhiteSpace(Color) ? string .Empty : $ "{Color} " ) .AppendLine($ "{Name}" ); foreach ( var child in Children) child.Print(sb, depth + 1); } /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { var sb = new StringBuilder(); Print(sb, 0); return sb.ToString(); } } /// <summary> /// /// </summary> public class Circle : GraphicObject { public override string Name => "圆" ; } /// <summary> /// /// </summary> public class Square : GraphicObject { public override string Name => "正方形" ; } |
调用:
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 | //组合模式 Console.WriteLine( "***组合模式Composite Pattern Demo. ***" ); #region Mathematics department //2 lecturers work in Mathematics department Employee mathTeacher1 = new Employee { Name = "老大" , Dept = "数学" , Designation = "讲师" }; Employee mathTeacher2 = new Employee { Name = "老二" , Dept = "数学" , Designation = "讲师" }; //The college has a Head of Department in Mathematics CompositeEmployee hodMaths = new CompositeEmployee { Name = "老六" , Dept = "数学" , Designation = "数学系主任" }; //Lecturers of Mathematics directly reports to HOD-Maths hodMaths.AddEmployee(mathTeacher1); hodMaths.AddEmployee(mathTeacher2); #endregion #region Computer Science department //3 lecturers work in Computer Sc. department Employee cseTeacher1 = new Employee { Name = "老三" , Dept = "计算机科学" , Designation = "讲师" }; Employee cseTeacher2 = new Employee { Name = "老四" , Dept = "计算机科学" , Designation = "讲师" }; Employee cseTeacher3 = new Employee { Name = "老五" , Dept = "计算机科学" , Designation = "讲师" }; //The college has a Head of Department in Computer science CompositeEmployee hodCompSc = new CompositeEmployee { Name = "老幺" , Dept = "计算机科学." , Designation = "计算机科学系主任" }; //Lecturers of Computer Sc. directly reports to HOD-CSE hodCompSc.AddEmployee(cseTeacher1); hodCompSc.AddEmployee(cseTeacher2); hodCompSc.AddEmployee(cseTeacher3); #endregion #region Top level management //The college also has a Principal CompositeEmployee principal = new CompositeEmployee { Name = "族长" , Dept = "规划监督管理" , Designation = "校长" }; //Head of Departments's of Maths and Computer Science directly reports to Principal. principal.AddEmployee(hodMaths); principal.AddEmployee(hodCompSc); #endregion /* * Printing the leaf-nodes and branches in the same way. * i.e. in each case, we are calling DisplayDetails() method. */ Console.WriteLine( "\nDetails of a Principal(校长) object is as follows:" ); //Prints the complete structure principal.DisplayDetails(); Console.WriteLine( "\nDetails of a HOD(系主任) object is as follows:" ); //Prints the details of Computer Science department hodCompSc.DisplayDetails(); //Leaf node Console.WriteLine( "\nDetails of an individual employee(leaf node一般教职员) is as follows:" ); mathTeacher1.DisplayDetails(); /* * Suppose, one Computer Science lecturer(老幺) * is leaving now from the organization. */ hodCompSc.RemoveEmployee(cseTeacher2); Console.WriteLine( "\n成员:" ); principal.DisplayDetails(); var drawing = new GraphicObject { Name = "我的画" }; drawing.Children.Add( new Square { Color = "红色" }); drawing.Children.Add( new Circle { Color = "黄色" }); var group = new GraphicObject(); group .Children.Add( new Circle { Color = "蓝色" }); group .Children.Add( new Square { Color = "蓝色" }); drawing.Children.Add( group ); Console.WriteLine(drawing); |
输出:
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 | ***组合模式Composite Pattern Demo. *** Details of a Principal(校长) object is as follows: 姓名:族长 工作在 规划监督管理 部门.描述:校长 姓名:老六 工作在 数学 部门.描述:数学系主任 姓名:老大 工作在 数学 部门.描述:讲师 姓名:老二 工作在 数学 部门.描述:讲师 姓名:老幺 工作在 计算机科学. 部门.描述:计算机科学系主任 姓名:老三 工作在 计算机科学 部门.描述:讲师 姓名:老四 工作在 计算机科学 部门.描述:讲师 姓名:老五 工作在 计算机科学 部门.描述:讲师 Details of a HOD(系主任) object is as follows: 姓名:老幺 工作在 计算机科学. 部门.描述:计算机科学系主任 姓名:老三 工作在 计算机科学 部门.描述:讲师 姓名:老四 工作在 计算机科学 部门.描述:讲师 姓名:老五 工作在 计算机科学 部门.描述:讲师 Details of an individual employee(leaf node一般教职员) is as follows: 姓名:老大 工作在 软件 部门.描述:讲师 成员: 姓名:族长 工作在 规划监督管理 部门.描述:校长 姓名:老六 工作在 数学 部门.描述:数学系主任 姓名:老大 工作在 软件 部门.描述:讲师 姓名:老二 工作在 软件 部门.描述:讲师 姓名:老幺 工作在 计算机科学. 部门.描述:计算机科学系主任 姓名:老三 工作在 计算机科学 部门.描述:讲师 姓名:老五 工作在 计算机科学 部门.描述:讲师 我的画 *红色 正方形 *黄色 圆 *组 **蓝色 圆 **蓝色 正方形 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!