-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample2.cpp
More file actions
47 lines (37 loc) · 1.17 KB
/
example2.cpp
File metadata and controls
47 lines (37 loc) · 1.17 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
#include <iostream>
#include <cassert>
#include "factory.h"
using namespace std;
class Animal {
public:
virtual std::string name() const=0;
};
class Duck : public Animal {
public:
std::string name() const override { return "Duck"; }
};
class Cow : public Animal {
public:
std::string name() const override { return "Cow"; }
};
class Snake : public Animal {
public:
std::string name() const override { return "Snake"; }
};
DECLARE_FACTORY_2(Animal, int);
REGISTER_FACTORY_2(Animal, Duck, int, 1);
REGISTER_FACTORY_2(Animal, Cow, int, 2);
REGISTER_FACTORY_2(Animal, Snake, int, 3);
DECLARE_FACTORY_2(Animal, string);
REGISTER_FACTORY_2(Animal, Duck, string, "Duck");
REGISTER_FACTORY_2(Animal, Cow, string, "Cow");
REGISTER_FACTORY_2(Animal, Snake, string, "Snake");
int main() {
assert(Factory<Animal>::build(1)->name() == "Duck");
assert(Factory<Animal>::build(2)->name() == "Cow");
assert(Factory<Animal>::build(3)->name() == "Snake");
assert(Factory<Animal>::build<string>("Duck")->name() == "Duck");
assert(Factory<Animal>::build<string>("Cow")->name() == "Cow");
assert(Factory<Animal>::build<string>("Snake")->name() == "Snake");
return 0;
}