c# 組合模式
更新時(shí)間:2012年10月29日 14:02:53 作者:
組合模式:將對(duì)象組合成樹形結(jié)構(gòu)以表示‘部分-整體’的層次結(jié)構(gòu)。組合模式使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。需求中式體現(xiàn)部分與整體層次的結(jié)構(gòu)時(shí),統(tǒng)一地使用組合對(duì)象中的所有對(duì)象時(shí),應(yīng)該考慮使用組合模式
結(jié)構(gòu)圖:

抽象對(duì)象:
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);
}
無(wú)子節(jié)點(diǎn)的:
class Leaf : Component
{
public Leaf(string name)
: base(name)
{ }
public override void Add(Component c)
{
//throw new NotImplementedException();
Console.WriteLine("Cannot add to a Leaf");
}
public override void Remove(Component c)
{
//throw new NotImplementedException();
Console.WriteLine("Cannot remove to a Leaf");
}
public override void Display(int depth)
{
//throw new NotImplementedException();
Console.WriteLine(new string('-', depth) + name);
}
}
可以有子結(jié)點(diǎn):
class Composite : Component
{
private List<Component> children = new List<Component>();
public Composite(string name)
: base(name)
{ }
public override void Add(Component c)
{
//throw new NotImplementedException();
children.Add(c);
}
public override void Remove(Component c)
{
//throw new NotImplementedException();
children.Remove(c);
}
public override void Display(int depth)
{
//throw new NotImplementedException();
Console.WriteLine(new string('-', depth) + name);
foreach (Component component in children)
{
component.Display(depth + 2);
}
}
}
主函數(shù)調(diào)用:
class Program
{
static void Main(string[] args)
{
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);
Composite comp2 = new Composite("Composite X");
comp2.Add(new Leaf("Leaf XYA"));
comp2.Add(new Leaf("Leaf XYB"));
comp.Add(comp2);
root.Display(1);
Console.ReadKey();
}
}

抽象對(duì)象:
復(fù)制代碼 代碼如下:
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);
}
無(wú)子節(jié)點(diǎn)的:
復(fù)制代碼 代碼如下:
class Leaf : Component
{
public Leaf(string name)
: base(name)
{ }
public override void Add(Component c)
{
//throw new NotImplementedException();
Console.WriteLine("Cannot add to a Leaf");
}
public override void Remove(Component c)
{
//throw new NotImplementedException();
Console.WriteLine("Cannot remove to a Leaf");
}
public override void Display(int depth)
{
//throw new NotImplementedException();
Console.WriteLine(new string('-', depth) + name);
}
}
可以有子結(jié)點(diǎn):
復(fù)制代碼 代碼如下:
class Composite : Component
{
private List<Component> children = new List<Component>();
public Composite(string name)
: base(name)
{ }
public override void Add(Component c)
{
//throw new NotImplementedException();
children.Add(c);
}
public override void Remove(Component c)
{
//throw new NotImplementedException();
children.Remove(c);
}
public override void Display(int depth)
{
//throw new NotImplementedException();
Console.WriteLine(new string('-', depth) + name);
foreach (Component component in children)
{
component.Display(depth + 2);
}
}
}
主函數(shù)調(diào)用:
復(fù)制代碼 代碼如下:
class Program
{
static void Main(string[] args)
{
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);
Composite comp2 = new Composite("Composite X");
comp2.Add(new Leaf("Leaf XYA"));
comp2.Add(new Leaf("Leaf XYB"));
comp.Add(comp2);
root.Display(1);
Console.ReadKey();
}
}
您可能感興趣的文章:
- Android源碼學(xué)習(xí)之組合模式定義及應(yīng)用
- JavaScript 設(shè)計(jì)模式之組合模式解析
- php設(shè)計(jì)模式 Composite (組合模式)
- C++設(shè)計(jì)模式之組合模式
- asp.net 組合模式的一個(gè)例子
- Java設(shè)計(jì)模式之組合模式(Composite模式)介紹
- C#組合模式實(shí)例詳解
- Python的組合模式與責(zé)任鏈模式編程示例
- iOS應(yīng)用開發(fā)中運(yùn)用設(shè)計(jì)模式中的組合模式的實(shí)例解析
- java設(shè)計(jì)模式之組合模式(Composite)
相關(guān)文章
C#難點(diǎn)逐個(gè)擊破(9):類型轉(zhuǎn)換
類型之間的轉(zhuǎn)換可以分為隱式轉(zhuǎn)換與顯式轉(zhuǎn)換,如int類型可直接轉(zhuǎn)換為long類型。2010-02-02C#基于WebSocket實(shí)現(xiàn)聊天室功能
這篇文章主要為大家詳細(xì)介紹了C#基于WebSocket實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C# 使用 WebBrowser 實(shí)現(xiàn) HTML 轉(zhuǎn)圖片功能的示例代碼
這篇文章主要介紹了C# 如何使用 WebBrowser 實(shí)現(xiàn) HTML 轉(zhuǎn)圖片功能,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07基于params,ref,out的參數(shù)問(wèn)題詳解
本篇文章是對(duì)params,ref,out的參數(shù)問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06C# Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例
本文主要介紹了C# Winform中DataGridView導(dǎo)出為Excel的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05