12.组合模式

设计模式——组合模式

一、基本概念

1. 定义

组合(Composite Pattern)模式的定义:有时又叫作整体-部分(Part-Whole)模式,它是一种将对象组合成树状的层次结构的模式,用来表示“整体-部分”的关系,使用户对单个对象和组合对象具有一致的访问性,属于结构型设计模式。

2. 优缺点

优点

  • 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;
  • 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;

缺点

  • 设计较复杂,客户端需要花更多时间理清类之间的层次关系;
  • 不容易限制容器中的构件;
  • 不容易用继承的方法来增加构件的新功能;

3. 结构

  • 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。(总的抽象类或接口,定义一些通用的方法,比如新增、删除)
  • 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于继承或实现抽象构件。
  • 树枝构件(Composite)角色 / 中间构件:是组合中的分支节点对象,它有子节点,用于继承和实现抽象构件。它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。
classDiagram class Component { <<abstract>> +calculation() float +show() void } class Leaf { +calculation() float +show() void } class Composite { -list~Component~ children +add(Component) void +remove(Component) void +getChild(int) Component +calculation() float +show() void } Component <|-- Leaf : 树叶 Component <|-- Composite : 树枝 Composite o--> Component : 管理子节点

二、代码实现

UML

classDiagram class Component { <<abstract>> +calculation() float +show() void } class Goods { -string name -int quantity -float price +calculation() float +show() void } class Pocket { -vector~Component~ pocket +add(Component) void +remove(Component) void +getChild(int) Component +calculation() float +show() void } Component <|-- Goods : 商品(树叶) Component <|-- Pocket : 口袋(树枝) Pocket o--> Component : 管理子节点

抽象结构角色

通用接口:

class Component {
public:
virtual ~Component() = default;
virtual float calculation() = 0;
virtual void show() = 0;
};

树叶构建角色

商品类:

#include <iostream>
#include <string>
class Goods : public Component {
public:
Goods(std::string name, int quantity, float price)
: name(std::move(name)), quantity(quantity), price(price) {}
float calculation() override {
return quantity * price;
}
void show() override {
std::cout << name << ":" << "(数量:" << quantity << "个;单价:" << price << "元)" << std::endl;
}
private:
std::string name;
int quantity;
float price;
};

具体建造者

口袋类:

#include <memory>
#include <string>
#include <vector>
class Pocket : public Component {
public:
explicit Pocket(std::string name) : name(std::move(name)) {}
void add(std::unique_ptr<Component> component) {
pocket.push_back(std::move(component));
}
void remove(Component* component) {
for (auto it = pocket.begin(); it != pocket.end(); ++it) {
if (it->get() == component) {
pocket.erase(it);
break;
}
}
}
Component* getChild(int i) const {
return pocket[i].get();
}
float calculation() override {
float sum = 0;
for (const auto& component : pocket) {
sum += component->calculation();
}
return sum;
}
void show() override {
for (const auto& component : pocket) {
component->show();
}
}
private:
std::string name;
std::vector<std::unique_ptr<Component>> pocket;
};

客户类

#include <iostream>
#include <memory>
int main() {
auto small = std::make_unique<Pocket>("小号袋子");
auto medium = std::make_unique<Pocket>("中号袋子");
auto large = std::make_unique<Pocket>("大号袋子");
small->add(std::make_unique<Goods>("苹果", 5, 1.3f));
small->add(std::make_unique<Goods>("香蕉", 8, 1.5f));
medium->add(std::move(small));
medium->add(std::make_unique<Goods>("牛奶", 3, 4.1f));
large->add(std::move(medium));
large->show();
std::cout << "总价:" << large->calculation() << "元" << std::endl;
}

运行结果:

苹果:(数量:5个;单价:1.3元)
香蕉:(数量:8个;单价:1.5元)
牛奶:(数量:3个;单价:4.1元)
总价:30.8元

参考:


Thanks for reading!

12.组合模式

2026-01-22
929 字 · 5 分钟

已复制链接

评论区

目录