-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfactory_method.hpp
More file actions
51 lines (42 loc) · 827 Bytes
/
factory_method.hpp
File metadata and controls
51 lines (42 loc) · 827 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
#ifndef FACTORY_METHOD_HPP
#define FACTORY_METHOD_HPP
#include <string>
#include <iostream>
using namespace std;
class Product {
public:
virtual string getName() = 0;
virtual ~Product() {}
};
class ConcreteProductA: public Product {
public:
string getName() {return "ConcreteProductA";}
};
class ConcreteProductB: public Product {
public:
string getName() {return "ConcreteProductB";}
};
class Creator {
public:
Product* GetProduct();
protected:
virtual Product* CreateProduct() = 0;
private:
Product* prod = nullptr;
};
template <typename tprod>
class ConcreteCreator : public Creator
{
protected:
virtual Product* CreateProduct()
{
return new tprod;
}
};
Product* Creator::GetProduct()
{
if(prod == nullptr)
prod = CreateProduct();
return prod;
}
#endif // FACTORY_METHOD_HPP