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);
}
無子節(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);
}
無子節(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# 實(shí)現(xiàn)視頻監(jiān)控系統(tǒng)(附源碼)
這篇文章主要介紹了C# 如何實(shí)現(xiàn)視頻監(jiān)控系統(tǒng),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02
C#開發(fā)微信門戶及應(yīng)用(3) 文本消息和圖文消息應(yīng)答
這篇文章主要為大家詳細(xì)介紹了C#開發(fā)微信門戶及應(yīng)用第二篇,微信文本消息和圖文消息的應(yīng)答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
使用C#實(shí)現(xiàn)一個(gè)簡單的繪圖工具
這篇文章主要為大家詳細(xì)介紹了如何使用C#開發(fā)的簡單繪圖工具,可以將簽名簡單繪圖后的效果以圖片的形式導(dǎo)出,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
unity實(shí)現(xiàn)鼠標(biāo)拖住3D物體
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)鼠標(biāo)拖住3D物體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
在Framework 4.0中:找出新增的方法與新增的類(二)
為什么動(dòng)態(tài)加載程序集無法找出Framework 4.0 和Framwork2.0 新增的方法和類2013-05-05
C# Winform 讓整個(gè)窗口都可以拖動(dòng)
Windows 的 API 果然強(qiáng)大啊.以前要實(shí)現(xiàn)全窗口拖動(dòng), 要寫鼠標(biāo)按下和抬起事件, 很是麻煩, 偶爾還會(huì)出現(xiàn) BUG2011-05-05
DataTables List互相轉(zhuǎn)換的實(shí)現(xiàn)類示例
這篇文章主要介紹了將DataTable轉(zhuǎn)換為List,將List轉(zhuǎn)換為DataTable的實(shí)現(xiàn)類實(shí)例方法,大家參考使用吧2013-11-11

