第34章
作者:吴强      更新:2024-09-06 05: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:“嘿嘿!不错喔!”