using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace 组合 { //负责人抽象类 public abstract class absHead { protected string name; public absHead(string name) { this.name = name; } //增加 public abstract void Add(absHead head); //删除 public abstract void Remove(absHead head); //显示 public abstract void Display(int depth); //履行职责 public abstract void LineofDuty(); } //具体负责人,树枝节点 public class Head:absHead { private List<absHead > headList=new List<absHead >(); public Head(string name) : base(name) { } public override void Add(absHead head) { headList.Add(head); } public override void Remove(absHead head) { headList.Remove(head); } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); foreach (absHead abshead in headList ) { abshead.Display(depth + 2); } } public override void LineofDuty() { foreach (absHead abshead in headList) { abshead.LineofDuty(); } } } //网络部门内部成员 树叶节点 public class Network:absHead { public Network(string name) : base(name) { } public override void Add(absHead head) { } public override void Remove(absHead head) { } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void LineofDuty() { Console.WriteLine("{0} 网络部门:负责机房的ip-Mac绑定、重装系统检查",name); } } //网线部门内部成员 树叶节点 public class Switch : absHead { public Switch(string name) : base(name) { } public override void Add(absHead head) { } public override void Remove(absHead head) { } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void LineofDuty() { Console.WriteLine("{0} 网线部门:负责机房网线的通畅", name); } } //服务器部门内部成员 树叶节点 public class Server : absHead { public Server(string name) : base(name) { } public override void Add(absHead head) { } public override void Remove(absHead head) { } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void LineofDuty() { Console.WriteLine("{0} 服务器部门:负责机房的监控、打印机、服务器", name); } } //工具部门内部成员 树叶节点 public class Tool : absHead { public Tool(string name) : base(name) { } public override void Add(absHead head) { } public override void Remove(absHead head) { } public override void Display(int depth) { Console.WriteLine(new string('-', depth) + name); } public override void LineofDuty() { Console.WriteLine("{0} 工具部门:负责机房所有工具的管理", name); } } class Program { static void Main(string[] args) { Head root = new Head("九期(许恕)"); Head comp = new Head("十期(罗智福)"); root.Add(comp); Head comp1 = new Head("十期(屈文哲)"); comp1.Add(new Network("十一期(周洲)")); comp.Add(comp1); Head comp2 = new Head("十期(孙一清)"); comp2.Add(new Switch("十一期(冯尧)")); comp.Add(comp2); Head comp3 = new Head("十期(邱慕夏)"); comp3.Add(new Server("十一期(张晗)")); comp.Add(comp3); Head comp4 = new Head("十期(王庆波)"); comp4.Add(new Tool("十一期(廖旭)")); comp.Add(comp4); Console.WriteLine("\n机房管理委员会结构图:"); root.Display(1); Console.WriteLine("\n机房管理委员会职责:"); root.LineofDuty(); Console.Read(); } } }
组合模式类图:
原文地址:http://blog.csdn.net/ry513705618/article/details/38361599