//Component模式 组合模式
enum NodeTag
{
LEFT,
COMPOSITE
};
class IComponent
{
protected:
IComponent(){}
IComponent(string name){this->m_szName=name;}
public:
virtual ~IComponent(){}
public:
virtual size_t Size(){return 0;};
virtual void Operate()=0;
virtual void RemoveAt(int index){}
virtual void Add(IComponent *pChild){}
virtual IComponent * GetChild(int index){return nullptr;}
virtual IComponent * GetChild(string name){return nullptr;}
string & GetName(){return m_szName;}
NodeTag GetNodeTag(){return m_nodeTag;}
protected:
string m_szName;
NodeTag m_nodeTag;
};
//叶子节点
class Leaf:public IComponent
{
public:
Leaf(){this->m_nodeTag=LEFT;}
Leaf(string name):IComponent(name){this->m_nodeTag=LEFT;}
~Leaf(){}
public:
void Operate(){printf("this is %s node!\n",m_szName.c_str());}
};
//分支节点
class Composite:public IComponent
{
public:
Composite(){this->m_nodeTag=COMPOSITE;}
Composite(string name):IComponent(name){this->m_nodeTag=COMPOSITE;}
~Composite(){}
public:
void Operate(){printf("this is %s node!\n",m_szName.c_str());}
size_t Size()
{
return components.size();
}
void RemoveAt(int index)
{
if(index>=0&&index<=components.size()-1)
{
components.erase(components.begin()+index);
}
}
void Add(IComponent *pChild)
{
if (!GetChild(pChild->GetName()))
{
components.push_back(pChild);
}
}
IComponent * GetChild(int index)
{
if(index>=0&&index<=components.size()-1)
{
return components[index];
}
else
{
return nullptr;
}
}
IComponent * GetChild(string name)
{
int i=-1;
for (;i<components.size();++i)
{
if (components[i]->GetName()==name)
{
break;
}
}
if (i!=-1)
{
return components[i];
}
else
{
return nullptr;
}
}
private:
vector<IComponent*> components;
};
//Component模式 组合模式
int _tmain(int argc, _TCHAR* argv[])
{
IComponent *pRootComponent0=new Composite("Root0");//第一层一个
IComponent *pRootComponent10=new Composite("Root10");//第2层一个
IComponent *pRootComponent20=new Composite("Root20");//第2层2个
IComponent *pRootComponent30=new Composite("Root30");//第2层3个
pRootComponent0->Add(pRootComponent10);
pRootComponent0->Add(pRootComponent20);
pRootComponent0->Add(pRootComponent30);
IComponent *pLeafs10[3]= //第2层一个 3个叶子节点
{
new Leaf("Root101"),
new Leaf("Root102"),
new Leaf("Root103"),
};
IComponent *pLeafs20[3]= //第2层2个 3个叶子节点
{
new Leaf("Root201"),
new Leaf("Root202"),
new Leaf("Root203"),
};
IComponent *pLeafs30[3]= //第2层2个 3个叶子节点
{
new Leaf("Root301"),
new Leaf("Root302"),
new Leaf("Root303"),
};
for (int k=0;k<3;k++)
{
pRootComponent10->Add(pLeafs10[k]);
pRootComponent20->Add(pLeafs20[k]);
pRootComponent30->Add(pLeafs30[k]);
}
//按层输出
queue<IComponent*>q;
q.push(pRootComponent0);
while (q.size()!=0)
{
IComponent* pic=q.front();
pic->Operate();
//出队
q.pop();
for (int i=0;i<pic->Size();++i)
{
//入队
q.push(pic->GetChild(i));
}
}
//递归输出
struct Loop
{
public:
static void RecursiveTravel(IComponent* ic)
{
if (ic)
{
ic->Operate();
for (int i=0;i<ic->Size();++i)
{
RecursiveTravel(ic->GetChild(i));
}
}
}
};
Loop::RecursiveTravel(pRootComponent0);
for (int k=0;k<3;k++)
{
delete pLeafs10[k];
delete pLeafs20[k];
delete pLeafs30[k];
}
delete pRootComponent10;
delete pRootComponent20;
delete pRootComponent30;
delete pRootComponent0;
//Component模式
getchar();
return 0;
}
转载于:https://my.oschina.net/u/221120/blog/889098