组合模式通过统一接口处理树形结构,适用于文件系统等“部分-整体”场景,其核心由Component、Leaf和Composite构成,实现递归操作与统一调用。

在C++中处理树形结构时,组合模式(Composite Pattern)是一种非常自然且高效的设计模式选择。它允许你将对象组合成树形结构来表示“部分-整体”的层次结构,同时使得客户端可以统一地处理单个对象和组合对象。这种模式特别适用于文件系统、组织架构、UI控件树等具有递归层级关系的场景。
组合模式主要由三个角色构成:
通过这个结构,客户端无需区分处理的是叶子还是容器,统一调用接口即可。
以下是一个用C++实现的简单文件系统示例,展示如何用组合模式处理树形结构:
立即学习“C++免费学习笔记(深入)”;
#include <iostream>
#include <vector>
#include <string>
<p>// 抽象组件类
class FileSystemComponent {
public:
virtual ~FileSystemComponent() = default;
virtual void display(int depth = 0) const = 0;
virtual long long size() const = 0;
};</p><p>// 叶子类:文件
class File : public FileSystemComponent {
private:
std::string name;
long long fileSize;</p><p>public:
File(const std::string& n, long long size) : name(n), fileSize(size) {}</p><pre class='brush:php;toolbar:false;'>void display(int depth = 0) const override {
std::string indent(depth * 2, ' ');
std::cout << indent << "? " << name << " (" << fileSize << " bytes)\n";
}
long long size() const override {
return fileSize;
}};
// 容器类:目录 class Directory : public FileSystemComponent { private: std::string name; std::vector<FileSystemComponent*> children;
public: Directory(const std::string& n) : name(n) {}
void add(FileSystemComponent* component) {
children.push_back(component);
}
void remove(FileSystemComponent* component) {
children.erase(
std::remove(children.begin(), children.end(), component),
children.end()
);
}
void display(int depth = 0) const override {
std::string indent(depth * 2, ' ');
std::cout << indent << "? " << name << "\n";
for (const auto& child : children) {
child->display(depth + 1);
}
}
long long size() const override {
long long total = 0;
for (const auto& child : children) {
total += child->size();
}
return total;
}};
使用示例:
int main() {
Directory root("Root");
Directory docs("Documents");
Directory pics("Pictures");
<pre class='brush:php;toolbar:false;'>File f1("report.docx", 1024);
File f2("photo.jpg", 2048);
File f3("notes.txt", 128);
docs.add(&f1);
docs.add(&f3);
pics.add(&f2);
root.add(&docs);
root.add(&pics);
root.display(); // 层级打印
std::cout << "Total size: " << root.size() << " bytes\n";
return 0;}
组合模式在处理树形结构时具备以下优点:
常见应用场景包括:
基本上就这些。组合模式通过统一抽象屏蔽了树形结构的复杂性,让C++开发者能更专注于业务逻辑而非结构判断。只要存在“整体-部分”关系,且需要统一操作,组合模式就是一种简洁有力的解决方案。
以上就是C++组合模式应用 树形结构处理方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号