-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate2.cpp
80 lines (65 loc) · 1.19 KB
/
template2.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <iostream>
using namespace std;
/**
* 库开发
*/
class Library
{
public:
void run()
{
step1();
while (!step2()) { // 多态调用
step3();
}
step4(); // 多态调用
step5();
}
virtual ~Library(){}
protected:
void step1()
{
cout << "Library.step1()" << endl;
}
void step3()
{
cout << "Library.step3()" << endl;
}
void step5()
{
cout << "Library.step5()" << endl;
}
private: // 设计习语:NVI:Non-Virtual Interface
virtual bool step2() = 0;
virtual int step4() = 0;
};
/**
* 应用开发
*/
class App : public Library
{
private:
bool step2() override
{
// Library::step2();
cout << "App.step2()" << endl;
number++;
return number >= 4;
}
int step4() override
{
cout << "App.step4()" << endl;
return number;
}
int number{0};
};
int main()
{
Library *pLib = new App();
// 1. 存储成本:虚表指针 + 虚表结构(共享)
// 2. 调用成本:间接指针,无法inline
pLib->run();
delete pLib;
return 0;
}