CComponent(const string &strName)
{
m_pstrName = strName;
}
virtual void Add(CComponent *const pc) = 0;
virtual void Remove(CComponent *const pc) = 0;
virtual void Display(const int &nDepth) = 0;
};
//Leaf 在组合中表示叶节点对象,叶节点没有子节点。
class CLeaf : public CComponent
{
public:
CLeaf(const string &strName) : CComponent(strName){}
void Add(CComponent *const pc)
{
cout<<"Cannot add to a leaf"<<endl;
}
void Remove(CComponent *const pc)
{
cout<<"Cannot remove from a leaf"<<endl;
}
void Display(const int &nDepth)
{
cout<<'-'<<nDepth<<':'<<m_pstrName<<endl;
}
};
//Composite 定义有枝节点行为,用来存储子部件,在 Component 接口中实现与
//子部件有关的操作,比如增加 Add 和阐述 Remove。
class CComposite : public CComponent
{
private:
set<CComponent*> m_Children;
public:
CComposite(const string &strName) : CComponent(strName)
{
m_Children.clear();
}
void Add(CComponent * const pC)
{
m_Children.insert(pC);
}
void Remove(CComponent * const pC)