-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.cpp
More file actions
70 lines (59 loc) · 1.16 KB
/
bridge.cpp
File metadata and controls
70 lines (59 loc) · 1.16 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
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
// 变化维度 1
class ILeft {
public:
virtual void func() = 0;
virtual ~ILeft() = default;
};
class Left1 : public ILeft {
public:
void func() override {
cout << "Left1" << endl;
}
};
class Left2 : public ILeft {
public:
void func() override {
cout << "Left2" << endl;
}
};
// 变化维度 2
class IRight {
public:
virtual void func() = 0;
virtual ~IRight() = default;
};
class Right1 : public IRight {
public:
void func() override {
cout << "Right1" << endl;
}
};
class Right2 : public IRight {
public:
void func() override {
cout << "Right2" << endl;
}
};
// 具有两个变换维度: Left 和 Right
// 当然也允许多个变化维度
class Bridge {
private:
ILeft* _left;
IRight* _right;
public:
Bridge(ILeft* left, IRight* right) : _left(left), _right(right) {}
void run() {
_left->func();
_right->func();
}
};
int main() {
Left1 left1;
Right2 right2;
// 运行时装配, 第一个变化维度使用 Left1, 第二个变化维度使用 Right2
Bridge b(&left1, &right2);
b.run();
return 0;
}