C++中的组合模式

作者 : admin 本文共3382个字,预计阅读时间需要9分钟 发布时间: 2024-06-11 共1人阅读

目录

组合模式(Composite Pattern)

实际应用

文件系统

组织结构

图形对象

总结


组合模式(Composite Pattern)

组合模式是一种结构型设计模式,它将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得客户端可以统一地处理单个对象和对象组合。这个模式特别适用于需要表示层次结构的场景,例如文件系统、组织结构等。

实际应用

组合模式的核心思想是将单个对象和组合对象都实现同一个接口,从而使得客户端可以一致地处理它们。

文件系统

实现一个文件系统,其中文件和目录都可以被统一处理。

#include 
#include 
#include 
#include 

// 抽象基类,表示文件系统中的一个节点
class FileSystemNode {
public:
    virtual ~FileSystemNode() = default;
    virtual void display(int depth = 0) const = 0;
};

// 叶子节点,表示文件
class File : public FileSystemNode {
private:
    std::string name;
public:
    File(const std::string& name) : name(name) {}

    void display(int depth = 0) const override {
        std::cout << std::string(depth, ' ') << "File: " << name << "
";
    }
};

// 组合节点,表示目录
class Directory : public FileSystemNode {
private:
    std::string name;
    std::vector<std::shared_ptr> children;
public:
    Directory(const std::string& name) : name(name) {}

    void add(const std::shared_ptr& node) {
        children.push_back(node);
    }

    void display(int depth = 0) const override {
        std::cout << std::string(depth, ' ') << "Directory: " << name <display(depth + 2);
        }
    }
};

int main() {
    auto root = std::make_shared("root");
    auto home = std::make_shared("home");
    auto user = std::make_shared("user");
    auto file1 = std::make_shared("file1.txt");
    auto file2 = std::make_shared("file2.txt");
    auto file3 = std::make_shared("file3.txt");

    root->add(home);
    home->add(user);
    user->add(file1);
    user->add(file2);
    root->add(file3);

    root->display();

    return 0;
}

组织结构

实现一个组织结构,其中员工和部门都可以被统一处理。

#include 
#include 
#include 
#include 

// 抽象基类,表示组织结构中的一个节点
class OrganizationComponent {
public:
    virtual ~OrganizationComponent() = default;
    virtual void display(int depth = 0) const = 0;
};

// 叶子节点,表示员工
class Employee : public OrganizationComponent {
private:
    std::string name;
public:
    Employee(const std::string& name) : name(name) {}

    void display(int depth = 0) const override {
        std::cout << std::string(depth, ' ') << "Employee: " << name << "
";
    }
};

// 组合节点,表示部门
class Department : public OrganizationComponent {
private:
    std::string name;
    std::vector<std::shared_ptr> members;
public:
    Department(const std::string& name) : name(name) {}

    void add(const std::shared_ptr& component) {
        members.push_back(component);
    }

    void display(int depth = 0) const override {
        std::cout << std::string(depth, ' ') << "Department: " << name <display(depth + 2);
        }
    }
};

int main() {
    auto company = std::make_shared("Company");
    auto hr = std::make_shared("HR");
    auto it = std::make_shared("IT");
    auto alice = std::make_shared("Alice");
    auto bob = std::make_shared("Bob");
    auto charlie = std::make_shared("Charlie");

    company->add(hr);
    company->add(it);
    hr->add(alice);
    it->add(bob);
    it->add(charlie);

    company->display();

    return 0;
}

图形对象

实现一个图形对象层次结构,其中基本图形(如圆形、矩形)和复合图形(由多个基本图形组成)都可以被统一处理。

#include 
#include 
#include 

// 抽象基类,表示图形
class Graphic {
public:
    virtual ~Graphic() = default;
    virtual void draw() const = 0;
};

// 叶子节点,表示圆形
class Circle : public Graphic {
public:
    void draw() const override {
        std::cout << "Drawing Circle
";
    }
};

// 叶子节点,表示矩形
class Rectangle : public Graphic {
public:
    void draw() const override {
        std::cout << "Drawing Rectangle
";
    }
};

// 组合节点,表示复合图形
class CompositeGraphic : public Graphic {
private:
    std::vector<std::shared_ptr> children;
public:
    void add(const std::shared_ptr& graphic) {
        children.push_back(graphic);
    }

    void draw() const override {
        for (const auto& child : children) {
            child->draw();
        }
    }
};

int main() {
    auto circle1 = std::make_shared();
    auto circle2 = std::make_shared();
    auto rectangle1 = std::make_shared();

    auto composite1 = std::make_shared();
    composite1->add(circle1);
    composite1->add(rectangle1);

    auto composite2 = std::make_shared();
    composite2->add(circle2);
    composite2->add(composite1);

    composite2->draw();

    return 0;
}

总结

组合模式使得单个对象和组合对象可以被统一处理。所以无论是文件系统、组织结构还是图形对象,组合模式都能很好地表示层次结构。

本站无任何商业行为
个人在线分享 » C++中的组合模式
E-->