-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioc_tests.cpp
75 lines (64 loc) · 1.43 KB
/
ioc_tests.cpp
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
#include <iostream>
using namespace std;
class interface
{
public:
virtual ~interface() = default;
virtual void print_test()=0;
virtual void print_hello_world() = 0;
};
class implementation1: public interface
{
public:
virtual void print_test()
{
cout<<"TEST!\n";
};
virtual void print_hello_world()
{
cout<<"HELLO WORLD!\n";
};
};
class implementation2: public interface
{
public:
virtual void print_test()
{
cout<<"test!\n";
};
virtual void print_hello_world()
{
print_test();
cout<<"hello world!\n";
};
};
class consumer
{
public:
consumer(interface *inject_dep):dep(inject_dep)
{
if(dep == nullptr){
throw std::invalid_argument("service must not be null");
}
};
~consumer(){};
void Print1()
{
dep->print_test();
};
void Print2()
{
dep->print_hello_world();
};
private:
interface *dep = nullptr;
};
int main(int argc, char **argv)
{
interface *class1 = new implementation1();
interface *class2 = new implementation2();
consumer consumer1(class1);
consumer1.Print1();
consumer1.Print2();
return 1;
}