第34章

大B:“下面是組合模式的結構圖。”

大B:“組合模式爲組合中的對象聲明接口,在適當的情況下,實現所有類共有接口的默認行爲。聲明一個接口用於訪問和管理組合模式的子部件。”

abstractclassComponent

{

protectedstringname;

publicComponent(stringname)

{

this.name=name;

}

publicabstractvoidAdd(ponentc);//通常都用Add和Remove方法來提供增加或移出樹葉或樹枝的功能

publicabstractvoidRemove(Componentc);

publicabstractvoidDisplay(indepth);

}

Leaf在組合中表示葉節點對象,葉節點沒有子節點

classLeaf:Component

{

publicLeaf(stringname):base(name)

{}

publicoverridevoidAdd(Componentc)

//由於葉節點沒有再增加分枝和樹葉,所以Add和Remove方法實現

{

Console.WriteLine(“Cannotaddtoaleaf”);

//它沒有意義,但這樣可以消除葉節點和枝節點對象在抽象層次的區別

}//它們具備完全一致的接口

publicoverridevoidRemove(Componentc)

{

Console.WriteLine(“Cannotremovetoaleaf”);

}

publicoverridevoidDisplay(intdepth)

{

//葉節點的具體方法,此處是顯示其名稱和級別

Console.WriteLine();

}

}

Composite定義有枝節點行爲,用來存儲子部件,在Component接口中實現與子部件有關的操作,比如增加Add和刪除。

classComposite:Component

{

privateList《Component》children=newList《Component》();

publicComposite(stringname):base(name)

{}

publicoverridevoidAdd(Componentc)

{

children.add(c);

}

publicoverridevoidRemove(Componentc)

{

children.Remove(c);

}

publicoverridevoidDisplay(intdepth)

{//顯示枝節點名稱,並對其下級進行遍歷

Console.WriteLine(newstring(‘-’,depth)+name);

foreach(Componentponentinchildren)

{

ponent.Display(depth+2);

}

}

}

客戶端代碼,能通過Component接口操作組合部件的對象

staticvoidMain(string[]args)

{

Componentroot=newComponent(“root”);

root.Add(newLeaf(“LeafA”));//生成樹根root,根上長出兩葉

root.Add(newLeaf(“LeafB”));//LeafA與。

Compositep=newComposite(“ComponsiteX”);

p.Add(newLeaf(“LeafXA”));

p.Add(newLeaf(“LeafXB”));

root.Add(p);

Compositep2=newComposite(“CompositeXY”);

p2.Add(newLeaf(“LeafXYA”));

p2.Add(newLeaf(“LeafXYB”));

p.Add(p2);

//根部又長出兩頁LeafC和LeafD,可惜LeafD沒有長牢,被風吹走了

root.Add(newLeaf(“Leafc”));

Leafleaf=newLeaf(“LeafD”);

root.Add(leaf);

root.Remove(leaf);

root,Display(1);//顯示大樹的樣子

}

顯示結果:

root

——leafA

——leafB

——CompositeX

——LeafXA

——LeafXB

——CompositeXY

——CompositeXYA

——CompositeXYB

——Leafc

大B:“現在你能用代碼以組合模式,試寫一下我給我女朋友買生日禮物。”

小A:“OK”

代碼:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceComposite

{

interfaceIGift

{

voidPay();

voidAdd(IGiftgift);

}

classGiftSingle:IGift

{

privatestringmname;

publicGiftSingle(stringname)

{

mname=name;

www• ttκa n• c o

}

publicvoidAdd(IGiftgift)

{

}

publicvoidPay()

{

Console.WriteLine(“我買了”+mname+“!hoho~”);

wωω★ тt kān★ C〇

}

};

classGiftComposite:IGift

{

privatestringmname;

List《IGift》mgifts;

publicGiftComposite()

{

mname=string.Empty;

mgifts=newList《IGift》();

}

publicvoidAdd(IGiftgift)

{

mgifts.Add(gift);

}

publicvoidPay()

{

foreach(IGiftgiftinmgifts)

{

gift.Pay();

}

}

};

classProgram

{

staticvoidMain(string[]args)

{

//20歲生日,那時的MM還很單純~

Console.WriteLine(“lalala~20歲生日來咯——”);

IGiftsingleGift20=newGiftSingle(“手錶”);

singleGift20.Pay();

//22歲生日,MM變得狡詐了~

Console.WriteLine(“heiheihei~22歲生日來咯——”);

IGiftpositeGift22=newGiftComposite();

//打包,打包!我要把所有喜歡的禮物打包成“一套”~

positeGift22.Add(newGiftSingle(“手機”));

positeGift22.Add(newGiftSingle(“DC”));

positeGift22.Add(newGiftSingle(“DV”));

positeGift22.Pay();

//24歲生日……天哪!

Console.WriteLine(“hiahiahia~24歲生日來咯——”);

//先逛商場一層~買化妝品!

IGiftpositeGift24=newGiftComposite();

//打包,打包!

positeGift24.Add(newGiftSingle(“香水”));

positeGift24.Add(newGiftSingle(“指甲油”));

positeGift24.Add(newGiftSingle(“眼影”));

//然後來到二層,看中了一套衣服~

IGiftsingleGift24=newGiftSingle(“衣服”);

//因爲只能買“一件”,所以“狡詐”的MM再次打包……

IGifttotalGifts=newGiftComposite();

//我包,我包,我包包包!

totalGifts.Add(positeGift24);

totalGifts.Add(singleGift24)。

totalGifts.Pay();

}

}

}

大B:“嘿嘿!不錯喔!”

第180章第28章第6章第209章第83章第139章第8章第185章第160章第120章第18章第218章第183章第70章第110章第100章第197章第133章第16章第109章第115章第69章第97章第37章第202章第124章第217章第45章第228章第219章第55章第92章第202章第170章第55章第41章第44章第224章第135章第28章第75章第136章第71章第14章第75章第141章第175章第186章第116章第215章第51章第36章第38章第206章第80章第32章第119章第40章第113章第205章第211章第223章第27章第32章第171章第17章第217章第60章第23章第167章第87章第50章第63章第107章第34章第191章第22章第11章第59章第220章第2章第157章第25章第108章第74章第89章第159章第75章第86章第168章第45章第139章第55章第138章第151章第96章第146章第189章第7章第217章
第180章第28章第6章第209章第83章第139章第8章第185章第160章第120章第18章第218章第183章第70章第110章第100章第197章第133章第16章第109章第115章第69章第97章第37章第202章第124章第217章第45章第228章第219章第55章第92章第202章第170章第55章第41章第44章第224章第135章第28章第75章第136章第71章第14章第75章第141章第175章第186章第116章第215章第51章第36章第38章第206章第80章第32章第119章第40章第113章第205章第211章第223章第27章第32章第171章第17章第217章第60章第23章第167章第87章第50章第63章第107章第34章第191章第22章第11章第59章第220章第2章第157章第25章第108章第74章第89章第159章第75章第86章第168章第45章第139章第55章第138章第151章第96章第146章第189章第7章第217章