C#设计模式---组合模式(Composite Pattern)
一、目的
让客户以统一的方式处理简单对象和复杂对象。
二、定义
组合模式允许客户将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户可以以一致的方式处理单个对象以及组合对象。
三、场景
假设我们要制作一个画图程序,所画的图形包括简单图形(直线,椭圆,三角形等)和复杂图形(椭圆+直线,直线+三角形等)两种。
为了能让客户已统一的方式来进行绘制简单图形和复杂图形,我们采用组合模式来实现。
四、实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 4 namespace DesignMode_Composite 5 { 6 public abstract class Graphics 7 { 8 public string Name { get; set; } 9 public Graphics(string Name) 10 { 11 this.Name = Name; 12 } 13 14 public abstract void Draw(); 15 public abstract void AddGraphics(Graphics g); 16 public abstract void RemoveGraphics(Graphics g); 17 } 18 19 /// <summary> 20 /// Simple graphics:Line 21 /// </summary> 22 public class Line: Graphics 23 { 24 public Line(string Name):base(Name) 25 { 26 27 } 28 public override void Draw() 29 { 30 Console.WriteLine("Drawing Line: " + Name); 31 } 32 public override void AddGraphics(Graphics g) 33 { 34 throw new NotImplementedException(); 35 } 36 public override void RemoveGraphics(Graphics g) 37 { 38 throw new NotImplementedException(); 39 } 40 } 41 /// <summary> 42 /// Simple graphics:Ellipse 43 /// </summary> 44 public class Ellipse:Graphics 45 { 46 public Ellipse(string Name):base(Name) 47 { 48 49 } 50 public override void Draw() 51 { 52 Console.WriteLine("Drawing Ellipse: " + Name); 53 } 54 public override void AddGraphics(Graphics g) 55 { 56 throw new NotImplementedException(); 57 } 58 public override void RemoveGraphics(Graphics g) 59 { 60 throw new NotImplementedException(); 61 } 62 63 } 64 /// <summary> 65 /// Simple graphics:Rectangle 66 /// </summary> 67 public class Rectangle:Graphics 68 { 69 public Rectangle(string Name):base(Name) 70 { 71 72 } 73 public override void Draw() 74 { 75 Console.WriteLine("Drawing Rectangle: " +Name); 76 } 77 public override void AddGraphics(Graphics g) 78 { 79 throw new NotImplementedException(); 80 } 81 public override void RemoveGraphics(Graphics g) 82 { 83 throw new NotImplementedException(); 84 } 85 } 86 /// <summary> 87 /// Complex graphics 88 /// </summary> 89 public class ComplexGraphics:Graphics 90 { 91 private List<Graphics> graphicsList; 92 public ComplexGraphics(string Name):base(Name) 93 { 94 graphicsList = new List<Graphics>(); 95 } 96 public override void Draw() 97 { 98 99 Console.ForegroundColor = (ConsoleColor)Math.Abs(new Random().Next(15)); 100 Console.WriteLine("Drawing Complex: " + Name); 101 Console.ResetColor(); 102 foreach (var g in graphicsList) 103 { 104 g.Draw(); 105 } 106 107 } 108 public override void AddGraphics(Graphics g) 109 { 110 graphicsList.Add(g); 111 } 112 public override void RemoveGraphics(Graphics g) 113 { 114 graphicsList.Remove(g); 115 } 116 } 117 class Program 118 { 119 static void Main(string[] args) 120 { 121 122 Console.ForegroundColor = ConsoleColor.DarkGreen; 123 Console.WriteLine("**********Welcome to use drawing tool**********"); 124 Console.ResetColor(); 125 126 List<Graphics> graphicsList = new List<Graphics>(); 127 Graphics smpGraphicsLineA = new Line("Line A");//Create a simple line A 128 Graphics smpGraphicsLineB = new Line("Line B");//Create a simple line B 129 Graphics smpGraphicsEllipse = new Ellipse("Ellipse A"); 130 Graphics smpGraphicsRectangle = new Rectangle("Rectangle A"); 131 132 Graphics comGraphicsA = new ComplexGraphics("Complex A"); 133 comGraphicsA.AddGraphics(smpGraphicsLineA); 134 comGraphicsA.AddGraphics(smpGraphicsEllipse); 135 136 Graphics comGraphicsB = new ComplexGraphics("Complex B"); 137 comGraphicsB.AddGraphics(smpGraphicsLineB); 138 comGraphicsB.AddGraphics(smpGraphicsRectangle); 139 140 graphicsList.Add(smpGraphicsLineA); 141 graphicsList.Add(smpGraphicsLineB); 142 graphicsList.Add(smpGraphicsEllipse); 143 graphicsList.Add(smpGraphicsRectangle); 144 graphicsList.Add(comGraphicsA); 145 graphicsList.Add(comGraphicsB); 146 147 foreach(var g in graphicsList) 148 { 149 g.Draw(); 150 } 151 152 } 153 } 154 }
运行结果如下:
五、类图
六、描述
组合模式中,涉及以下三个元素:
(1)抽象组件(Component)角色:这是一个抽象角色,上面的类Graphics充当这个角色,它定义了Leaf和Composite公共的接口,可以用来管理所有的子对象。
(2)树叶组件(Leaf)角色:树叶对象没有子级对象,上面实现中类Line和Ellipse和Rectangle充当这个角色。
(3)树枝组件(Composite)角色:树枝组件有子级对象,上面实现的ComplexGraphics充当这个角色,树枝对象给出所有管理子对象的方法实现,如AddGraphics、RemoveGraphics。