结构型模式——组合模式

组合设计模式适用于以下情况:

• 你想表示对象的部分------整体层次结构;

• 你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象;

下面是一个组合模式的示例程序:

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <memory>

// 1. The Component Interface ------ Defines common operations for both files and folders
class FileSystemNode {
protected:
    std::string name_;
public:
    FileSystemNode(const std::string& name) : name_(name) {}
    virtual ~FileSystemNode() = default;

    virtual void display(int depth = 0) const = 0;
};

// 2. The Leaf Node ------ Represents individual files that cannot contain anything else
class File : public FileSystemNode {
private:
    int size_; // Unique attribute for files

public:
    File(const std::string& name, int size) : FileSystemNode(name), size_(size) {}

    void display(int depth = 0) const override {
        // Print spaces based on the tree depth for indentation
        std::string indent(depth * 2, ' ');
        std::cout << indent << "- 📄 File: " << name_ << " (" << size_ << " KB)\n";
    }
};

// 3. The Composite Node ------ Represents folders that can contain both files and other folders
class Folder : public FileSystemNode {
private:
    // A collection storing child nodes polymorphically
    std::vector<std::unique_ptr<FileSystemNode>> children_;

public:
    Folder(const std::string& name) : FileSystemNode(name) {}

    // Method to add items into the folder
    void add(std::unique_ptr<FileSystemNode> node) {
        children_.push_back(std::move(node));
    }

    void display(int depth = 0) const override {
        std::string indent(depth * 2, ' ');
        std::cout << indent << "📁 Folder: " << name_ << "\n";

        // Recursively trigger display() on all children without worrying about their exact type
        for (const auto& child : children_) {
            child->display(depth + 1); 
        }
    }
};

// 4. Client Code
int main() {
    // Create the top-level root folder
    auto rootFolder = std::make_unique<Folder>("Root");

    // Create standalone files and a sub-folder
    auto file1 = std::make_unique<File>("Config.ini", 4);
    auto subFolder = std::make_unique<Folder>("Projects");
    
    auto projectFile1 = std::make_unique<File>("main.cpp", 12);
    auto projectFile2 = std::make_unique<File>("README.md", 2);

    // Assemble the tree structure
    subFolder->add(std::move(projectFile1));
    subFolder->add(std::move(projectFile2));

    rootFolder->add(std::move(file1));
    rootFolder->add(std::move(subFolder)); // Nesting a folder inside a folder

    // Uniform treatment: Call display on the root, and the entire tree renders automatically
    std::cout << "--- Printing Directory Tree ---\n";
    rootFolder->display();

    return 0;
}

程序运行结果如下:

shell 复制代码
$ g++ -o main main.cpp
$ ./main
--- Printing Directory Tree ---
📁 Folder: Root
  - 📄 File: Config.ini (4 KB)
  📁 Folder: Projects
    - 📄 File: main.cpp (12 KB)
    - 📄 File: README.md (2 KB)
相关推荐
努力努力再努力wz2 小时前
【分布式系统与 RPC 框架系列】从单机瓶颈到远程调用:一文理解分布式架构与 RPC 原理
linux·网络·c++·分布式·网络协议·rpc·架构
神仙别闹2 小时前
基于C++实现(控制台)景区旅游管理系统
开发语言·c++·旅游
workflower2 小时前
供应链分销网络选址问题
人工智能·机器学习·设计模式·自然语言处理·机器人
hold?fish:palm13 小时前
kv存储主从复制的设计与实现
c++·redis·后端
啦啦啦啦啦zzzz14 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法
charlie11451419114 小时前
现代C++工程实践 WeakPtr 实战(三):WeakPtrFactory 与「最后成员」惯用法
开发语言·c++·开源项目·现代c++
JetComXCpp16 小时前
给 Win32 窗口加上毛玻璃效果——纯 D3D11 实现,零依赖
c++
ysa05103018 小时前
【板子】二分答案(最大最小?)
c++·笔记·算法·板子
是多巴胺不是尼古丁18 小时前
C++基础知识
开发语言·c++