0%

组合模式

组合模式简介

组合模式将对象组合成树状结构

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#define MAX_CHILD_COMPONENT_COUNT 10
class Component {
public:
virtual void execute() = 0;
};

class SimpleComponent: public Component {
public:
void execute() override {
//execute
}
};

class CompositeComponent: public Component {
private:
Component* children[MAX_CHILD_COMPONENT_COUNT];
public:
void add(Component* child) {

}

void remove(Component *child) {

}

void execute() override {
for (auto child : children) {
child->execute();
}
}
};