第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;

}

publicvoidAdd(IGiftgift)

{

}

publicvoidPay()

{

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

}

};

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:“嘿嘿!不錯喔!”