UML 类图
内容目录

类图的几种关系

  1. Association 关联
  2. Dependency 依赖
  3. Aggregation 聚合
  4. Composition 组合
  5. Inheritance 继承
  6. Implementation 实现

关联

单向关联

class Teacher {
public:
    Teacher(const std::string& name) : name(name) {}
    std::string getName() const { return name; }

private:
    std::string name;
};

class Student {
public:
    Student(const std::string& name, Teacher* teacher) : name(name), teacher(teacher) {}

    void showTeacher() const {
        if (teacher) {
            std::cout << "Teacher: " << teacher->getName() << std::endl;
        }
    }

private:
    std::string name;
    Teacher* teacher;  // 学生与教师之间的关联关系
};

双向关联


class Course;  // 前向声明

class Student {
public:
    Student(const std::string& name) : name(name) {}

    void enroll(Course* course) {
        this->course = course;  // 学生与课程之间的关联关系
    }

private:
    std::string name;
    Course* course;
};

class Course {
public:
    Course(const std::string& title) : title(title) {}

    void addStudent(Student* student) {
        students.push_back(student);  // 课程与学生之间的关联关系
    }

private:
    std::string title;
    std::vector<Student*> students;  // 存储多个学生
};

依赖

通常表示为一个类的某个方法使用了另一个类的对象或接口,但并不长期持有该对象。这是一种临时的、松散的关联关系,通常表现为在方法的参数中传递对象。


class Document {
public:
    Document(const std::string& content) : content(content) {}

    std::string getContent() const {
        return content;
    }

private:
    std::string content;
};

class Printer {
public:
    // Printer 依赖于 Document 的内容来进行打印
    void print(const Document& doc) const {
        std::cout << "Printing Document: " << doc.getContent() << std::endl;
    }
};

聚合

通常用于描述一个类包含另一个类的对象,但它们的生命周期是相互独立的。换句话说,聚合表示"整体-部分"的关系,其中整体(拥有者)和部分(被拥有的对象)可以独立存在。


class Employee {
public:
    Employee(const std::string& name) : name(name) {}

    std::string getName() const {
        return name;
    }

private:
    std::string name;
};

class Department {
public:
    Department(const std::string& name) : name(name) {}

    void addEmployee(Employee* employee) {
        employees.push_back(employee);  // 部门拥有员工的指针
    }

    void showEmployees() const {
        std::cout << "Department: " << name << std::endl;
        for (const auto& employee : employees) {
            std::cout << "- Employee: " << employee->getName() << std::endl;
        }
    }

private:
    std::string name;
    std::vector<Employee*> employees;  // 使用指针表示聚合关系
};

组合

表示一种更强的“拥有”关系,其中一个类的对象完全依赖于另一个类的对象。组合关系强调整体与部分之间的生命周期关系:当整体对象被销毁时,部分对象也会被销毁。


class Engine {
public:
    Engine(int horsepower) : horsepower(horsepower) {}

    int getHorsepower() const {
        return horsepower;
    }

private:
    int horsepower;
};

class Car {
public:
    Car(const std::string& model, int horsepower) 
        : model(model), engine(horsepower) {}  // Car 创建 Engine 对象

    void showCar() const {
        std::cout << "Car model: " << model 
                  << ", Engine horsepower: " << engine.getHorsepower() << std::endl;
    }

private:
    std::string model;
    Engine engine;  // 组合关系:Car 拥有 Engine
};
上一篇
下一篇