-
Notifications
You must be signed in to change notification settings - Fork 0
/
0.builder_robot.h
128 lines (117 loc) · 2.68 KB
/
0.builder_robot.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Created by Tianyi Zhang on 9/24/20.
//
#ifndef DESIGN_PATTERN_ROBOTPLAN_H
#define DESIGN_PATTERN_ROBOTPLAN_H
#include <iostream>
#include <string>
#include <memory>
class RobotPlan{
public:
virtual void setRobotHead(std::string const& head) = 0;
virtual void setRobotLeg(std::string const& leg) = 0;
virtual void getRobotHead() = 0;
virtual void getRobotLeg() = 0;
virtual void getInfo() = 0;
};
class Robot : public RobotPlan {
public:
std::string head;
std::string leg;
void setRobotHead(std::string const& head) override{
this->head = head;
}
void setRobotLeg(std::string const& leg) override{
this->leg = leg;
}
void getRobotHead() override{
std::cout<<head<<std::endl;
}
void getRobotLeg() override{
std::cout<<leg<<std::endl;
}
void getInfo() override{
getRobotHead();
getRobotLeg();
}
};
class OldRobotSpec{
public:
Robot robot;
void buildRobotHead() {
robot.setRobotHead("oldHead");
}
void buildRobotLeg() {
robot.setRobotLeg("oldLeg");
}
Robot getRobot() {
return robot;
}
};
class NewRobotSpec{
public:
Robot robot;
void buildRobotHead(){
robot.setRobotHead("newHead");
}
void buildRobotLeg(){
robot.setRobotLeg("newLeg");
}
Robot getRobot() {
return robot;
}
};
class RobotSpec{
public:
template<typename T>
RobotSpec(T robotSpec){
self = std::make_shared<RobotSpecModel<T>>(robotSpec);
}
void buildRobotHead(){
self->buildRobotHead();
}
void buildRobotLeg(){
self->buildRobotLeg();
}
Robot getRobot(){
return self->getRobot();
}
private:
class RobotSpecConcept {
public:
virtual void buildRobotHead() = 0;
virtual void buildRobotLeg() = 0;
virtual Robot getRobot() = 0;
};
template<typename T>
class RobotSpecModel: public RobotSpecConcept{
public:
RobotSpecModel(T const& robotSpec){
this->robotSpec = robotSpec;
}
void buildRobotHead(){
robotSpec.buildRobotHead();
}
void buildRobotLeg(){
robotSpec.buildRobotLeg();
}
Robot getRobot(){
return robotSpec.getRobot();
}
T robotSpec;
};
std::shared_ptr<RobotSpecConcept> self;
};
class RobotBuilder{
public:
RobotBuilder(RobotSpec const& robotSpec):robotSpec(robotSpec){}
void makeRobot(){
robotSpec.buildRobotHead();
robotSpec.buildRobotLeg();
}
Robot getRobot(){
return robotSpec.getRobot();
}
RobotSpec robotSpec;
};
#endif //DESIGN_PATTERN_ROBOTPLAN_H