设计模式学习笔记--组合模式

 1 using System;
 2 
 3 namespace Composite
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/30 7:11:10 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Component说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public abstract class Component
12     {
13         protected string name;
14 
15         public Component(string name)
16         {
17             this.name = name;
18         }
19 
20         public abstract void Add(Component c);
21         public abstract void Remove(Component c);
22         public abstract void Display(int depth);
23     }
24 }
View Code
 1 using System;
 2 
 3 namespace Composite
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/30 7:13:28 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Leaf说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Leaf : Component
12     {
13         public Leaf(string name)
14             : base(name)
15         {
16         }
17 
18         public override void Add(Component c)
19         {
20             Console.WriteLine("Cannot add to a leaf");
21         }
22 
23         public override void Remove(Component c)
24         {
25             Console.WriteLine("Cannot remove from a leaf");
26         }
27 
28         public override void Display(int depth)
29         {
30             Console.WriteLine(new string('-', depth) + name);
31         }
32     }
33 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 
 4 namespace Composite
 5 {
 6     /// <summary> 
 7     /// 作者:bzyzhang
 8     /// 时间:2016/5/30 7:15:49 
 9     /// 博客地址:http://www.cnblogs.com/bzyzhang/
10     /// Composite说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
11     /// </summary> 
12     public class Composite : Component
13     {
14         private List<Component> children = new List<Component>();
15 
16         public Composite(string name)
17             : base(name)
18         {
19         }
20 
21         public override void Add(Component c)
22         {
23             children.Add(c);
24         }
25 
26         public override void Remove(Component c)
27         {
28             children.Remove(c);
29         }
30 
31         public override void Display(int depth)
32         {
33             Console.WriteLine(new string('-', depth) + name);
34             foreach (Component component in children)
35             {
36                 component.Display(depth + 2);
37             }
38         }
39     }
40 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Composite
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Composite root = new Composite("root");
14             root.Add(new Leaf("Leaf A"));
15             root.Add(new Leaf("Leaf B"));
16 
17             Composite comp = new Composite("Composite X");
18             comp.Add(new Leaf("Leaf XA"));
19             comp.Add(new Leaf("Leaf XB"));
20 
21             root.Add(comp);
22 
23             Composite comp2 = new Composite("Composite XY");
24             comp2.Add(new Leaf("Leaf XYA"));
25             comp2.Add(new Leaf("Leaf XYB"));
26 
27             root.Add(comp2);
28 
29             root.Add(new Leaf("Leaf C"));
30 
31             Leaf leaf = new Leaf("Leaf D");
32             root.Add(leaf);
33             root.Remove(leaf);
34 
35             root.Display(1);
36         }
37     }
38 }
View Code
原文地址:https://www.cnblogs.com/bzyzhang/p/5541044.html