-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample1.cpp
More file actions
36 lines (29 loc) · 759 Bytes
/
example1.cpp
File metadata and controls
36 lines (29 loc) · 759 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
#include <iostream>
#include <cassert>
#include "factory.h"
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(Animal, int);
REGISTER_FACTORY(Animal, Duck, 1);
REGISTER_FACTORY(Animal, Cow, 2);
REGISTER_FACTORY(Animal, Snake, 3);
int main() {
assert(Factory<Animal>::build(1)->name() == "Duck");
assert(Factory<Animal>::build(2)->name() == "Cow");
assert(Factory<Animal>::build(3)->name() == "Snake");
return 0;
}