生活随笔
收集整理的這篇文章主要介紹了
合成模式(Composite)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
模式定義
合成(Composite)模式:將對象組合成樹形結構以表示“部分-整體”的層次結構。Composite使得用戶對單個對象和組合對象的使用具有一致性。
UML類圖
public static class CompositeApp{public static void Run(){Composite root = new Composite("root");root.Add(new Leaf("Leaf A"));root.Add(new Leaf("Leaf B"));Composite comp = new Composite("Composite X");comp.Add(new Leaf("Leaf XA"));comp.Add(new Leaf("Leaf XB"));root.Add(comp);root.Add(new Leaf("Leaf C"));root.Display(1);Console.ReadKey();}}abstract class Component{protected string name;public Component(string name){this.name = name;}public abstract void Add(Component c);public abstract void Remove(Component c);public abstract void Display(int depth);}class Composite : Component{private List<Component> _children = new List<Component>();public Composite(string name) : base(name){}public override void Add(Component c){_children.Add(c);}public override void Display(int depth){Console.WriteLine(new string('-', depth) + name);foreach (Component component in _children){component.Display(depth + 2);}}public override void Remove(Component c){_children.Remove(c);}}class Leaf : Component{public Leaf(string name): base(name){}public override void Add(Component c){Console.WriteLine("Cannot add to a leaf");}public override void Remove(Component c){Console.WriteLine("Cannot remove from a leaf");}public override void Display(int depth){Console.WriteLine(new String('-', depth) + name);}}
情景案例
這里以畫一副畫為例,一幅畫有好多元素組成,每種元素都有展示方法Display(int indent);。但有的是獨立的元素(樹葉節點),有的是組合元素(樹枝節點)。
其實Asp.Net WebForm 中的Control就是組合機構,有個控件可以包含其它控件
public static class MainApp{public static void Run(){CompositeElement root = new CompositeElement("Picture");root.Add(new PrimitiveElement("Red Line"));root.Add(new PrimitiveElement("Blue Circle"));root.Add(new PrimitiveElement("Green Box"));CompositeElement comp = new CompositeElement("Two Circles");comp.Add(new PrimitiveElement("Black Circle"));comp.Add(new PrimitiveElement("White Circle"));root.Add(comp);PrimitiveElement pe = new PrimitiveElement("Yellow Line");root.Add(pe);root.Remove(pe);root.Display(1);Console.ReadKey();}}/// <summary>/// 畫元素/// </summary>abstract class DrawingElement{protected string _name;public DrawingElement(string name){this._name = name;}public abstract void Add(DrawingElement d);public abstract void Remove(DrawingElement d);public abstract void Display(int indent);}/// <summary>/// 單獨元素/// </summary>class PrimitiveElement : DrawingElement{public PrimitiveElement(string name): base(name){}public override void Add(DrawingElement c){Console.WriteLine("Cannot add to a PrimitiveElement");}public override void Remove(DrawingElement c){Console.WriteLine( "Cannot remove from a PrimitiveElement");}public override void Display(int indent){Console.WriteLine(new String('-', indent) + " " + _name);}}/// <summary>/// 組合元素/// </summary>class CompositeElement : DrawingElement{private List<DrawingElement> elements = new List<DrawingElement>();public CompositeElement(string name) : base(name){}public override void Add(DrawingElement d){elements.Add(d);}public override void Remove(DrawingElement d){elements.Remove(d);}public override void Display(int indent){Console.WriteLine(new String('-', indent) + "+ " + _name);foreach (DrawingElement d in elements){d.Display(indent + 2);}}}
轉載于:https://www.cnblogs.com/LoveTomato/p/8400928.html
總結
以上是生活随笔為你收集整理的合成模式(Composite)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。