-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.factory_ufo.h
60 lines (54 loc) · 1.23 KB
/
1.factory_ufo.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
#ifndef DESIGN_PATTERN_1_FACTORY_UFO_H
#define DESIGN_PATTERN_1_FACTORY_UFO_H
#include <string>
#include <iostream>
#include <memory>
class UFO{
public:
virtual void displayUFO() = 0;
virtual void setName(std::string name) = 0;
virtual void setSpeed(int speed) = 0;
};
class UFOImpl : public UFO{
public:
void displayUFO() override{
std::cout<<"name: "<<name<<" speed:"<<speed<<std::endl;
}
void setName(std::string name) override{
this->name = name;
}
void setSpeed(int speed) override{
this->speed = speed;
}
private:
std::string name;
int speed;
};
class FakeUFO: public UFOImpl{
public:
FakeUFO(){
setName("fakeUFO");
setSpeed(1);
}
};
class RealUFO: public UFOImpl{
public:
RealUFO(){
setName("realUFO");
setSpeed(100);
}
};
class UFOFactory{
public:
std::shared_ptr<UFO> makeUFO(std::string type){
if(type == "F") {
std::shared_ptr<UFO> fakeUFO = std::shared_ptr<FakeUFO>(new FakeUFO());
return fakeUFO;
}
else if(type == "R"){
auto realUFO = std::shared_ptr<RealUFO>(new RealUFO());
return realUFO;
}
}
};
#endif //DESIGN_PATTERN_1_FACTORY_UFO_H