-
class file
-
{
-
public:
-
file()
-
{
-
-
}
-
~file()
-
{
-
-
}
-
virtual void show()
-
{
-
std::list<file*>::iterator itr = m_Treelist.begin();
-
for (; itr != m_Treelist.end(); itr++)
-
{
-
(*itr)->show();
-
}
-
}
-
virtual void insert(file* tmp)
-
{
-
m_Treelist.push_back(tmp);
-
}
-
private:
-
std::list<file*> m_Treelist;
-
};
-
-
class realFile : public file{
-
public:
-
realFile() :file()
-
{
-
-
}
-
~realFile()
-
{
-
-
}
-
-
void show()
-
{
-
printf("I am a file \n");
-
}
-
};
-
-
class folder : public file
-
{
-
public:
-
folder() :file()
-
{
-
-
}
-
~folder()
-
{
-
-
}
-
-
};
-
file *root = new folder();
-
file *file1 = new realFile();
-
file *highmov = new folder();
-
root->insert(file1);
-
file *highfile = new realFile();
-
highmov->insert(highfile);
-
root->insert(highmov);
-
root->show();
组合模式解耦了客户程序与复杂元素内部结构,从而使客户程序可以向处理简单元素一样来处理复杂元素。
如果你想要创建层次结构,并可以在其中以相同的方式对待所有元素,那么组合模式就是最理想的选择。
阅读(1726) | 评论(0) | 转发(0) |