-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.composite_pattern.h
54 lines (48 loc) · 1.53 KB
/
16.composite_pattern.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
46
47
48
49
50
51
52
53
54
//
// Created by Tianyi Zhang on 3/4/21.
//
#ifndef DESIGN_PATTERN_16_COMPOSITE_PATTERN_H
#define DESIGN_PATTERN_16_COMPOSITE_PATTERN_H
#include <memory>
#include <iostream>
#include <vector>
#include <string>
#include <map>
//The composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies.
// Composite lets clients treat individual objects and compositions of objects uniformly
//make menu easier to extend
struct MenuComponent{
MenuComponent(std::string const& name, double price): name_(name), price_(price){};
MenuComponent(std::string const& name): name_(name){};
void add(MenuComponent const& menuComponent){
menuComponents_.push_back(menuComponent);
}
void print() const{
if(!isLeaf()){
std::cout<<"--------------------------"<<std::endl;
std::cout<<name_<<std::endl;
std::cout<<"--------------------------"<<std::endl;
}else{
std::cout<<name_<<","<<price_<<std::endl;
}
for(auto menuComponent : menuComponents_){
menuComponent.print();
}
}
private:
bool isLeaf() const{
return menuComponents_.size() == 0;
}
std::vector<MenuComponent> menuComponents_;
std::string name_;
double price_;
};
struct Waitress{
Waitress(MenuComponent const& menuComponent): menuComponent_(menuComponent) {}
void printMenu(){
menuComponent_.print();
}
private:
MenuComponent menuComponent_;
};
#endif //DESIGN_PATTERN_16_COMPOSITE_PATTERN_H