-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.compositePattern.h
45 lines (40 loc) · 1.11 KB
/
8.compositePattern.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// Created by Tianyi Zhang on 12/12/20.
//
#ifndef DESIGN_PATTERN_8_COMPOSITEPATTERN_H
#define DESIGN_PATTERN_8_COMPOSITEPATTERN_H
#include <string>
#include <vector>
#include <memory>
struct ToDoList{
virtual std::string getContext() = 0;
};
struct ToDo : ToDoList{
ToDo(std::string const& content) : content(content){}
std::string getContext() override{
std::string context;
context += "<beginItem>";
context += content;
context +=" <EndItem>\n";
return context;
}
private:
std::string content;
};
struct Project : public ToDoList{
Project(std::string const& projName, std::vector<std::shared_ptr<ToDoList>> const& todos) :
name(projName), todos(todos){}
std::string getContext() override {
std::string context;
context += "<beginItem>(" + name +")\n";
for(auto todo : todos){
context += todo->getContext();
}
context +="<EndItem>(" + name +")\n";
return context;
}
private:
std::string name;
std::vector<std::shared_ptr<ToDoList>> todos;
};
#endif //DESIGN_PATTERN_8_COMPOSITEPATTERN_H