-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdip.cpp
More file actions
54 lines (42 loc) · 1.07 KB
/
dip.cpp
File metadata and controls
54 lines (42 loc) · 1.07 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
47
48
49
50
51
52
53
54
//
// Created by Nelson Lin.
// The Dependency Inversion Principle
// 1) High-level modules (CoffeeMachine) should not depend on low-level modules. Both should depend on abstractions (e.g. interfaces).
// 2) Abstractions should not depend on details. Details (concrete implementations) should depend on abstractions. ( brew(Coffee) )
//
//
#include <iostream>
using namespace std;
struct Coffee {
virtual string to_string() = 0;
virtual ~Coffee() {
}
};
struct Arabia : Coffee {
string to_string() override {
return "arabia";
}
};
struct Rabusta : Coffee {
string to_string() override {
return "rabusta";
}
};
struct CoffeeMachine {
virtual void brew(unique_ptr<Coffee> coffee) = 0;
virtual ~CoffeeMachine() {
}
};
struct BrewMachine : CoffeeMachine {
void brew(unique_ptr<Coffee> coffee) override {
cout << "Brew: " << coffee->to_string() << endl;
}
};
int main() {
BrewMachine bm;
bm.brew(make_unique<Arabia>());
bm.brew(make_unique<Rabusta>());
cout << endl;
getchar();
return 0;
}