日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

合成模式(Composite)

發布時間:2025/4/14 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 合成模式(Composite) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

模式定義

合成(Composite)模式:將對象組合成樹形結構以表示“部分-整體”的層次結構。Composite使得用戶對單個對象和組合對象的使用具有一致性。

UML類圖

  • 抽象構件(component): 為參加組合的對象定義出公共的接口及其默認的行為,用來管理所有的子對象。負責對整體操作的方法有:Operation() Add(Component) Remove(Component) GetChild(int)
  • 樹枝構件(Composite): 抽象構建子類,并包含抽象構建類型的集合(存放下級對象)。參加組合的有下級子對象的對象,除擁有Operation()外,還包括整體操作方法:Add(Component) Remove(Component) GetChild(int)。
  • 樹葉構件(Left): 抽象構建子類,沒有下級子對象的對象,擁有Operation()即可

    代碼結構

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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。