-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDependencyInversion.cpp
99 lines (90 loc) · 2.41 KB
/
DependencyInversion.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @cite Dependency Inversion Principle states that high-level modules shouldn't depend on low-level modules rather both should depend on abstractions or interfaces also abstractions shouldn't depend on details rather details should depend on abstractions.
* Aims at Extensibility and Modularity as the codebase becomes less coupled.
*
* @brief Dependency Inversion Principle can be exemplified by a Animal interface and a AnimalSpeaker class that closely depends on an extension of Animal.
* What happens when an extension of Animal that has a different implementation is passed to AnimalSpeaker?
*/
#include <string>
#include <vector>
#include <iostream>
/**
* @brief Generic Animal Class or an Animal interface
*
*/
class Animal
{
public:
virtual void make_noise() = 0;
};
/**
* @brief Extension of Animal that stores noise as a `std::string`.
* Low-level Module that depends on abstractions
*/
class Dog : public Animal
{
public:
std::string noise = "Woof!!";
void make_noise() override
{
std::cout << noise << std::endl;
}
};
/**
* @brief Extension of Animal that stores noise as a `char[]`.
* Low-level Module that depends on abstractions
*/
class Cat : public Animal
{
public:
char noise[10] = "Meow!!";
void make_noise() override
{
std::cout << noise << std::endl;
}
};
/**
* @brief High-level Module that depends on low-level module.
* !@warning Closely coupled with Dog extension of Animal class
*/
class AnimalSpeaker
{
public:
AnimalSpeaker(Animal *animal)
{
// ! Imitates tight-coupling by only working if the object is of dog type
Dog *dog = dynamic_cast<Dog*>(animal);
if(!dog) throw std::runtime_error("Not a dog object !!!");
dog->make_noise();
}
};
/**
* @brief High-level module that depends on abstraction.
*/
class BetterAnimalSpeaker
{
public:
BetterAnimalSpeaker(Animal *animal)
{
animal->make_noise();
}
};
int main()
{
Dog dog;
Cat cat;
std::vector<Animal *> animals = {&dog, &cat};
for(auto animal: animals){
try
{
// ! @warning This results in an error as AniamlSpeaker is tightly coupled with Dog sub-class of Animal
AnimalSpeaker _(animal);
}
catch(const std::exception& e)
{
std::cerr << "Error :: " << e.what() << '\n';
}
BetterAnimalSpeaker __(animal);
}
return 0;
}