-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfactorymethod.cpp
62 lines (44 loc) · 874 Bytes
/
factorymethod.cpp
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
#include <vector>
#include <iostream>
#include <boost/shared_ptr.hpp>
using std::cout;
using std::endl;
enum bird_type{
FlappyBird,
AngryBird,
MyBird,
};
class bird{
public:
virtual void sing() = 0;
static boost::shared_ptr<bird> genBird(bird_type i);
};
class flappybird:public bird{
public:
void sing(){cout<<"flappybird"<<endl;}
};
class angrybird:public bird{
public:
void sing(){cout<<"angrybird"<<endl;}
};
class mybird:public bird{
public:
void sing(){cout<<"mybird"<<endl;}
};
boost::shared_ptr<bird> bird::genBird(bird_type i){
switch(i){
case FlappyBird:
return boost::shared_ptr<bird>(new flappybird());
break;
case AngryBird:
return boost::shared_ptr<bird>(new angrybird());
break;
case MyBird:
return boost::shared_ptr<bird>(new mybird());
break;
}
}
int main(){
auto b = bird::genBird(FlappyBird);
b->sing();
}