0
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| #include <iostream>
#include <string>
#include <map>
#include <boost/shared_ptr.hpp>
template <class base>
class factory
{
public:
typedef boost::shared_ptr<base> base_ptr;
template <class derived>
void reg(std::string const& name)
{
factories[name] = base_type_ptr(new derived_type<derived>);
}
base_ptr create(std::string const& name)
{
return factories[name]->create();
}
private:
class base_type
{
public:
virtual ~base_type() {}
virtual base_ptr create() const = 0;
};
typedef boost::shared_ptr<base_type> base_type_ptr;
template <class T>
class derived_type : public base_type
{
public:
virtual base_ptr create() const
{
return new T;
}
};
typedef std::map<std::string, base_type_ptr> factory_container;
factory_container factories;
};
class furniture
{
public:
virtual ~furniture() {}
virtual void show() = 0;
};
class table : public furniture
{
public:
virtual void show()
{
std::cout << "The big black wooden table" << std::endl;
}
};
class chair : public furniture
{
public:
virtual void show()
{
std::cout << "The old rocking chair" << std::endl;
}
};
int main()
{
typedef factory<furniture> furniture_factory;
typedef furniture_factory::base_ptr furniture_ptr;
furniture_factory fct;
fct.reg<table>("Table");
fct.reg<chair>("Chair");
furniture_ptr f1 = fct.create("Table");
furniture_ptr f2 = fct.create("Chair");
f1->show();
f2->show();
return 0;
} | |