-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_method.cpp
More file actions
59 lines (45 loc) · 986 Bytes
/
template_method.cpp
File metadata and controls
59 lines (45 loc) · 986 Bytes
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
#include <iostream>
using namespace std;
class ITemplate {
public:
virtual ~ITemplate() = default;
virtual void func1() = 0;
void func2() {
cout << "func2" << endl;
}
void func3() {
cout << "func3" << endl;
}
virtual void func4() = 0;
// Template Method 具有稳定的结构
void run() {
func1();
func2();
func3();
func4();
}
};
class Template1 : public ITemplate {
void func1() override {
cout << "Template1::func1" << endl;
}
void func4() override {
cout << "Template1::func4" << endl;
}
};
class Template2 : public ITemplate {
void func1() override {
cout << "Template2::func1" << endl;
}
void func4() override {
cout << "Template2::func4" << endl;
}
};
int main() {
ITemplate* temp1 = new Template1();
temp1->run();
cout << endl;
ITemplate* temp2 = new Template2();
temp2->run();
return 0;
}