设计模式中结构型模式(三)组合模式(Composite)

意图:将对象组合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个
对象和组合对象的使用具有一致性。
以下情况使用Composite模式
你想表示对象的部分-整体层次结构
你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

部分示例代码:

这部分内容由于有迭代器夹在中间,感觉有一定难度,难度不在于如何实现迭代器,而在于如何补齐对应的迭代器,使其能够满足程序的测试要求。

既使这样,程序还是稍微有点与书中不同,原因是书中的代码我想破脑袋也无法实现,猜测可能是写错了。

另外,其中有一个头文件是引用BasicClass.h中的,这里面包含了关于List表的部分实现,及其相关ListIterator的实现,在对应的文件中已作了修改(链接:http://blog.****.net/luhouxiang/archive/2008/04/17/2301849.aspx)。需要可到相关页面更新。

到今天,组合模式终于基本了结。

以下为相关类的类图:

设计模式中结构型模式(三)组合模式(Composite)

代码描述的是一组计算机结构。大体如下:机箱(Cabinet),主板(chassis),总线(bus),磁盘(FloppyDisk),

总线下的各种板卡(Card),程序采用树形结构将这些零件组合成一个计算机,最后给出计算整个机器的价钱的方法。

以下为整个计算机的组成结构,和类图稍有不同

Cabinet(机箱)

|

chassis(主板)

| |

bus(总线) FloppyDisk(磁盘)

|

Card(板卡)

图形从上往下读,机箱下面是主板,主板下包含总线和磁盘,总线下包含板卡

代码部分详细描述了计算机组成的树形结构的表示方法,并利用这种树形结构计算各部件的价钱总数:

设计模式中结构型模式(三)组合模式(Composite)//Composite.h:interfacefortheCCompositeclass.
设计模式中结构型模式(三)组合模式(Composite)
//
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
/**///////////////////////////////////////////////////////////////////////
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
#if!defined(AFX_COMPOSITE_H__4A747028_E312_4DFD_8E52_F9E48CCD5929__INCLUDED_)
设计模式中结构型模式(三)组合模式(Composite)
#defineAFX_COMPOSITE_H__4A747028_E312_4DFD_8E52_F9E48CCD5929__INCLUDED_
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
#if_MSC_VER>1000
设计模式中结构型模式(三)组合模式(Composite)
#pragmaonce
设计模式中结构型模式(三)组合模式(Composite)
#endif//_MSC_VER>1000
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)#include
"../BaseClass/BasicClass.h"
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=481289160296
设计模式中结构型模式(三)组合模式(Composite)
typedeffloatCurrency;
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602A3
设计模式中结构型模式(三)组合模式(Composite)
typedeffloatWatt;
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602B1
设计模式中结构型模式(三)组合模式(Composite)
classEquipment
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{//应该是一个类似抽象类
设计模式中结构型模式(三)组合模式(Composite)
public:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602B2
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
Equipment()...{_name="";}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602B3
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtual~Equipment()...{}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602B5
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
constchar*Name()const...{return_name;}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//virtualWattPower(){}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602B7
设计模式中结构型模式(三)组合模式(Composite)
virtualCurrencyNetPrice()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)Iterator
<ListNode<Equipment*>*>*i=CreateIterator();
设计模式中结构型模式(三)组合模式(Composite)Currencytotal
=0;
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)total
+=GetCurrency();
设计模式中结构型模式(三)组合模式(Composite)
for(i->First();!i->IsNull();i->Next())
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)total
+=i->CurrentItem()->_data->NetPrice();//NetPrice();
设计模式中结构型模式(三)组合模式(Composite)
}

设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)deletei;
设计模式中结构型模式(三)组合模式(Composite)
returntotal;
设计模式中结构型模式(三)组合模式(Composite)
return0;
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602B9
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualCurrencyDiscountPrice()...{return0;}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602C2
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualvoidAdd(Equipment*)...{}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602C5
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualvoidRemove(Equipment*)...{}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602C8
设计模式中结构型模式(三)组合模式(Composite)
virtualIterator<ListNode<Equipment*>*>*CreateIterator()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)ListNode
<Equipment*>*listpoint=_equipmentlist.FirstPoint();
设计模式中结构型模式(三)组合模式(Composite)Iterator
<ListNode<Equipment*>*>*newit=newListIterator<ListNode<Equipment*>*>(listpoint);
设计模式中结构型模式(三)组合模式(Composite)
returnnewit;
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//protected:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602CA
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
Equipment(constchar*name)...{_name=name;}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812D23D02DA
设计模式中结构型模式(三)组合模式(Composite)
voidSetCurrency(Currencycur)//设置价钱,每一个构造函数必须调用
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)_currency
=cur;
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812D23D0318
设计模式中结构型模式(三)组合模式(Composite)
CurrencyGetCurrency()const
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)
return_currency;
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
protected:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812D23D0338
设计模式中结构型模式(三)组合模式(Composite)
List<Equipment*>_equipmentlist;
设计模式中结构型模式(三)组合模式(Composite)
private:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602CC
设计模式中结构型模式(三)组合模式(Composite)
constchar*_name;
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812D23D0348
设计模式中结构型模式(三)组合模式(Composite)
Currency_currency;//价钱
设计模式中结构型模式(三)组合模式(Composite)
}
;
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602D1
设计模式中结构型模式(三)组合模式(Composite)
classFloppyDisk:publicEquipment
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)
public:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602D3
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
FloppyDisk(constchar*name):Equipment(name)...{SetCurrency(4.6);}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602D5
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtual~FloppyDisk()...{}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602E1
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualWattPower()...{printf("FloppyDisk::Power() ");return0;}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602E3
设计模式中结构型模式(三)组合模式(Composite)
virtualCurrencyNetPrice()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)printf(
"FloppyDisk::NetPrice() ");
设计模式中结构型模式(三)组合模式(Composite)
returnEquipment::NetPrice();
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602E5
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualCurrencyDiscountPrice()...{printf("FloppyDisk::DiscountPrice() ");return0;}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812D23D0376
设计模式中结构型模式(三)组合模式(Composite)
virtualIterator<ListNode<Equipment*>*>*CreateIterator()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)printf(
"FloppyDisk::CreateIterator() ");
设计模式中结构型模式(三)组合模式(Composite)
returnEquipment::CreateIterator();
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)}
;
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602F0
设计模式中结构型模式(三)组合模式(Composite)
classCompositeEquipment:publicEquipment
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)
public:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602F2
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtual~CompositeEquipment()...{}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602F4
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualWattPower()...{printf("CompositeEquipment::Power() ");return0;}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602F6
设计模式中结构型模式(三)组合模式(Composite)
virtualCurrencyNetPrice()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)printf(
"CompositeEquipment::NetPrice() ");
设计模式中结构型模式(三)组合模式(Composite)
returnEquipment::NetPrice();
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602F8
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualCurrencyDiscountPrice()...{printf("CompositeEquipment::DiscountPrice() ");return0;}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812891602FA
设计模式中结构型模式(三)组合模式(Composite)
virtualvoidAdd(Equipment*peq)
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)_equipmentlist.Append(peq);
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=481289160300
设计模式中结构型模式(三)组合模式(Composite)
virtualvoidRemove(Equipment*peq)
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)_equipmentlist.Remove(peq);
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=481289160303
设计模式中结构型模式(三)组合模式(Composite)
virtualIterator<ListNode<Equipment*>*>*CreateIterator()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)printf(
"CompositeEquipment::CreateIterator() ");
设计模式中结构型模式(三)组合模式(Composite)
returnEquipment::CreateIterator();
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
protected:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=481289160305
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
CompositeEquipment(constchar*sz):Equipment(sz)...{}
设计模式中结构型模式(三)组合模式(Composite)
private:
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)}
;
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//现在我们将计算机的底盘表示为CompositeEquipment的子类Chassis
设计模式中结构型模式(三)组合模式(Composite)
//它从CompositeEquipment继承了与子类有关的那些操作。
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=48128916031F
设计模式中结构型模式(三)组合模式(Composite)
classChassis:publicCompositeEquipment
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)
public:
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=481289160321
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
Chassis(constchar*sz):CompositeEquipment(sz)...{SetCurrency(8);}
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=48128916032E
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtual~Chassis()...{}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=481289160330
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
virtualWattPower()...{printf("Chassis::Power() ");return0;}
设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=481289160332
设计模式中结构型模式(三)组合模式(Composite)
virtualCurrencyNetPrice()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)printf(
"Chassis::NetPrice() ");
设计模式中结构型模式(三)组合模式(Composite)
returnEquipment::NetPrice();
设计模式中结构型模式(三)组合模式(Composite)}

设计模式中结构型模式(三)组合模式(Composite)
设计模式中结构型模式(三)组合模式(Composite)
//##ModelId=4812D23D03B4
设计模式中结构型模式(三)组合模式(Composite)
virtualIterator<ListNode<Equipment*>*>*CreateIterator()
设计模式中结构型模式(三)组合模式(Composite)设计模式中结构型模式(三)组合模式(Composite)
...{
设计模式中结构型模式(三)组合模式(Composite)