-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactoryMethod_II.h
More file actions
59 lines (51 loc) · 1.45 KB
/
Copy pathFactoryMethod_II.h
File metadata and controls
59 lines (51 loc) · 1.45 KB
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
//
// 工厂方法模式 —— 雷锋工厂
//
#ifndef DESIGNPATTERN_CPP_FACTORYMETHOD_II_H
#define DESIGNPATTERN_CPP_FACTORYMETHOD_II_H
#include <iostream>
// 雷锋
class LeiFeng {
public:
void Sweep() {
std::cout << "扫地" << std::endl;
}
void Wash() {
std::cout << "洗衣" << std::endl;
}
void BuyRice() {
std::cout << "买米" << std::endl;
}
};
// 学雷锋的大学生
class Undergraduate: public LeiFeng {};
// 社区志愿者
class Volunteer: public LeiFeng {};
// 雷锋工厂
class IFactory_LF {
public:
virtual LeiFeng* CreateLeiFeng() = 0;
};
// 学雷锋的大学生工厂
class UndergraduateFactory: public IFactory_LF {
public:
LeiFeng* CreateLeiFeng() override {
return new Undergraduate();
}
};
// 社区志愿者工厂
class VolunteerFactory: public IFactory_LF {
public:
LeiFeng* CreateLeiFeng() override {
return new Volunteer();
}
};
/**********************************************************
* // 工厂方法 *
* IFactory_LF* factory = new UndergraduateFactory(); *
* LeiFeng* student = factory->CreateLeiFeng(); *
* student->BuyRice(); *
* student->Sweep(); *
* student->Wash(); *
**********************************************************/
#endif //DESIGNPATTERN_CPP_FACTORYMETHOD_II_H