-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory_template.cpp
More file actions
45 lines (38 loc) · 826 Bytes
/
factory_template.cpp
File metadata and controls
45 lines (38 loc) · 826 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
#include <iostream>
template<typename Product>
class Factory {
public:
template<typename... Args>
static Product create(Args&&... args) {
return Product(std::forward<Args>(args)...);
}
};
class Product1 {
private:
int _x, _y;
public:
Product1(int x, int y) : _x(x), _y(y) {}
void func() const {
std::cout << "Product1: " << _x << ", " << _y << std::endl;
}
};
class Product2 {
private:
int _x, _y;
public:
Product2(int x, int y) : _x(x), _y(y) {}
void func() const {
std::cout << "Product2: " << _x << ", " << _y << std::endl;
}
};
// test 模块摆脱固定产品依赖
template<typename Product>
void test() {
Product nw = Factory<Product>::create(1, 1);
nw.func();
}
int main() {
test<Product1>();
test<Product2>();
return 0;
}